add class to body if 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 user is logged in

The robot generated the following code:

You can prefix functions with 'nm_add' by adding it to your function name. Here you want to add a class to the body if the user is logged in. You can use the 'body_class' filter for this in WordPress. Here's a code snippet for your requirements:

“`php
function nm_add_class_to_body( $classes ) {
if ( is_user_logged_in() ) {
$classes[] = 'nm_add';
}
return $classes;
}
add_filter( 'body_class', 'nm_add_class_class_to_body' );
“`

In this code, the 'nm_add' is added to the body class when the user is logged in. If the user is not logged, the function simply returns the existing classes without making any changes.

More
CODE SNIPPETS