By default when a user is logged in, a 'logged-in' classname is appended to the body tag. I need to append something like 'guest-user' to the body to output different styling based on whether or not the user is logged in.

How can I achieve this? Something maybe I could put in my child theme's functions.php file?



Yes, there's the body_class filter. You could use e.g.

function body_class_guest_user( $classes, $class ) {
    if ( ! is_user_logged_in() ) {
        $classes[] = 'guest-user';
    return $classes;
add_filter( 'body_class', 'body_class_guest_user', 10, 2 );

However you could equally well just use the :not() selector, e.g. body:not(.logged-in) (browser support), or absence of logged-in i.e. set up the guest-user styling as default but then override it if .logged-in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.