2

Here's what I am doing right now.

In my header.php file:

    <?php
        if ( current_user_can( 'subscriber' ) ){
            textdomain_body_classes();
        }
        else {
            relax();
        }
    ?>

In my functions.php class:

function textdomain_body_classes( $classes ) {
$classes[] = 'class-name';
return $classes;
}
add_filter( 'body_class', 'textdomain_body_classes' );

function relax() {
}

I am getting the class, class-name in my body tag when I login as admin as well as when I login with subscriber. Please advice. Thanks.

2

Please try this code instead-

add_filter( 'body_class', 'wpse_268176_body_class' );

function wpse_268176_body_class( $classes ) {

    $user = wp_get_current_user();

    if ( in_array( 'subscriber', $user->roles ) ) {
        $classes[] = 'class-name'; // your custom class name
    }

    return $classes;
}

Place this in your active theme's functions.php file.

[Thanks Dave Romsey for your suggestion.]

| improve this answer | |
  • 1
    Passing role names to current_user_can() is not advised. See notes section. Instead, do something like this (pardon formatting please) $user = wp_get_current_user(); if ( in_array( 'subscriber', $user->roles ) ) { $classes[] = 'class-name'; } – Dave Romsey May 26 '17 at 5:30

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.