add class to body if user logged in

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

team requested the  following code from my WordPress AI Code Generator.

add class to body if user logged in

The robot generated the following code:

Here is a WordPress code snippet for adding a class to the body if the user is logged in, with functions prefixed by 'nm_add'.

“`php
// Add a filter to the body_class function
add_filter('body_class','nm_add_logged_in_class');

/**
* This function adds the 'logged-in' class to the body class array
* if the user is logged in.
*
* @param array $classes – existing body classes.
*
* @return array $classes – modified body classes.
*/
function nm_add_logged_in_class($classes) {

if ( is_user_logged_in() ) {
// Add 'logged-in' class to the $classes array
$classes[] = 'logged-in';
}
return $classes;
}
“`

In this code, we define a function 'nm_add_logged_in_class' that adds the class 'logged-in' to the existing body classes if the user is logged in. This function is then added as a filter to 'body_class', so that it is called whenever the body class is generated.

More
CODE SNIPPETS