Logged in and Logged out Body Classes

Today I wound up needing a logged-out body class on a WordPress site I’m building. Any individual who has ever coded a WordPress sites has probably included the body_class() capacity to their websites and it would look something like the following:

<body <?php body_class(); ?>>

Well, the body_class() function permits you to effectively include classes specifically into the function like the following:

<body <?php body_class('class-name');?> >

Obviously, using this logic it will add the “Logged-out” class to the body tag on every page/post/custom taxonomy page across my website. You can include other conditional functions to only show the the class name on certain types of pages such as if a post is in a certain category.

// Add specific CSS class by filter
add_filter('body_class','er_logged_in_filter');
function er_logged_in_filter($classes) {
if( is_user_logged_in() ) {
$classes[] = 'loggedin-product-class';
} else {
$classes[] = 'loggedout-product-class';
}
// return the $classes array
return $classes;
}

Sometimes we need to Call Different Body Classes. Like when we want to call a class only who are logged in user and who are visitor or not logged in. We can use the above code in functions.php in wordpress to execute the body classes.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
0 Shares
Tweet
Share
Pin
Share