add class to body if a user is logged in

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

{Name (First):7.3} requested the  following code from my WordPress AI Code Generator.

add class to body if a user is logged in

The robot generated the following code:

Here is a code snippet to add a custom class to the body tag if a user is logged in with a prefix of 'nm_add' to all the functions:

In your functions.php file within your WordPress theme, you can add the following code snippet:

“`php
function nm_add_custom_body_class($classes) {
if(is_user_logged_in()) {
$classes[] = 'user-logged-in';
}

return $classes;
}

add_filter('body_class', 'nm_add_custom_body_class');
“`

With this code, when a user is logged in, a class named 'user-logged-in' will be added to the body element of the page. You can change the string `'user-logged-in'` to any other string to use a different class name.

More
CODE SNIPPETS