{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
add body class to site when user is logged in
The robot generated the following code:
Based on your requirement, here is the WordPress code snippet that adds a body class to your site when the user is logged in. We'll be using the `body_class` filter hook and prefixing the function with `nm_` as requested.
“`php
function nm_add_body_class_to_site( $classes ) {
// checks if user is logged in
if( is_user_logged_in() ) {
$classes[] = 'logged-in'; // add 'logged-in' to the $classes array
}
return $classes;
}
add_filter( 'body_class', 'nm_add_body_class_to_site' );
“`
In this piece of code, a function named `nm_add_body_class_to_site` has been created. It checks whether a user is logged in, and if the user is logged in, it adds 'logged-in' class to the body of the site. The function is then attached to the `body_class` filter hook.
When you inspect the page, you will see `.logged-in` class added in the body tag when you are logged in.