Add CSS Class on WordPress Body Tag

Insert code inside functions.php to initiate function that add class base on current user role:

 * WordPress function that add class on body tag by current user role.
function wp_add_user_role_to_body( $classes ) {
    global $current_user;
    $current_user_role  = $current_user->roles;
    $current_user_class = 'role-' . $current_user_role[0];

    // Check if user can view backend.
    if( is_admin() || current_user_can( 'edit_dashboard' ) || 
        is_customize_preview() ) {
        return $classes . $current_user_class;

    $classes[] = $current_user_class;
    return $classes;

// WordPress filter add class - frontend.
add_filter( 'body_class', 'wp_add_user_role_to_body' );

// WordPress filter add class - admin backend.
add_filter( 'admin_body_class', 'wp_add_user_role_to_body' );

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.