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

The robot generated the following code:

You can add a filter to the `body_class` hook to add a custom class to the body tag if a user is logged in. Below is the code snippet.

“`
<?php
// Add to your theme's functions.php file

if ( ! function_exists( 'nm_add_body_class' ) ) {
function nm_add_body_class( $classes ) {
if ( is_user_logged_in() ) {
$classes[] = 'user-logged-in';
}
return $classes;
}
add_filter( 'body_class', 'nm_add_body_class' );
}
?>
“`

When the user is logged in, the body tag will include the 'user-logged-in' class. In your CSS, you can then use the `.user-logged-in` classname to style the body tag differently for logged in users.

More
CODE SNIPPETS