Skip to content

Instantly share code, notes, and snippets.

@jancbeck
Last active January 15, 2022 19:34
Show Gist options
  • Save jancbeck/3178689 to your computer and use it in GitHub Desktop.
Save jancbeck/3178689 to your computer and use it in GitHub Desktop.
WordPress: Add user role class to body tag
<?php
// Add role class to body
function add_role_to_body($classes) {
foreach (wp_get_current_user()->roles as $user_role) {
$classes[] = 'role-'. $user_role;
}
return $classes;
});
add_filter('body_class','add_role_to_body');
add_filter('admin_body_class', 'add_role_to_body');
?>
@marcochiesi
Copy link

In my case (WP 3.9.1) the above code was not working, but the following did the job:

add_filter('body_class','add_role_to_body');
function add_role_to_body($classes) {
    $current_user = new WP_User(get_current_user_id());
    $user_role = array_shift($current_user->roles);
    $classes[] = 'role-'. $user_role;
    return $classes;
}

@bryanwillis
Copy link

currently works on latest wordpress (Version 4.4.2).

Here's my take on @marcochiesi version. Works on latest wordpress (Version 4.4.2) in the admin and the frontend and only runs the filter if the user is actually logged in.

if ( is_user_logged_in() ) {
    add_filter('body_class','add_role_to_body');
    add_filter('admin_body_class','add_role_to_body');
}
function add_role_to_body($classes) {
    $current_user = new WP_User(get_current_user_id());
    $user_role = array_shift($current_user->roles);
    if (is_admin()) {
        $classes .= 'role-'. $user_role;
    } else {
        $classes[] = 'role-'. $user_role;
    }
    return $classes;
}

@momekh
Copy link

momekh commented Feb 9, 2016

Logged in just to thank you for writing the code that just works. Thanks @bryanwillis

@gega23
Copy link

gega23 commented Feb 10, 2016

Me too! Saved my day. Thanks @bryanwillis

@graemebryson
Copy link

Cheers @bryanwillis - perfect.

@aconitewill
Copy link

I had to make an account on here just so I could say thank you to @bryanwillis - this is EXACTLY what I needed.

@zebrastribe
Copy link

Thanx @bryanwillis.

@costindeveloper
Copy link

Hi,
I which file do i have to put the above code? thx

@vithefiddler
Copy link

vithefiddler commented Sep 13, 2016

This is a great snippet of code. Thanks for posting it. @costindeveloper This code belongs in the functions.php file. I used the @bryanwillis version and it worked like a champ. Thanks @bryanwillis.

@grayayer
Copy link

grayayer commented Sep 22, 2016

I modified it so it also includes the user's id, and if this is placed in a functionality plugin, that it deals with the pluggability problem :

function add_helpful_user_classes() {
    if ( is_user_logged_in() ) {
        add_filter('body_class','class_to_body');
        add_filter('admin_body_class', 'class_to_body_admin');
    }
}
add_action('init', 'add_helpful_user_classes');

/// Add user role class to front-end body tag
function class_to_body($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    $classes[] = $user_role.' ';
    return $classes;
}

/// Add user role class and user id to front-end body tag

// add 'class-name' to the $classes array
function class_to_body_admin($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    /* Adds the user id to the admin body class array */
    $user_ID = $current_user->ID;
    $classes .= $user_role.' '.'user-id-'.$user_ID ;
    return $classes;
    return 'user-id-'.$user_ID;
}

@Garconis
Copy link

With WordPress, users can technically have more than one Role. It looks like this is only pulling in the "first" role. Is there a way to display ALL roles, each as a new class?

@great2gether
Copy link

I am glad this came into somebodys mind :) Awesome way to hide and show certain areas of wordpress depending on the user roles. I am using Wordpress 4.7.3 right now and @bryanwillis ' code snippet is doing a great job adding the role class to the body tag. Coooooool! :) Thank you very much guys!

@AzeemAnsari
Copy link

where to add this code guys?

@joanna-s
Copy link

joanna-s commented May 3, 2018

I found this didn't play well with the theme I'm using (if it were up to me I'd make my own themes but i'm not the boss unfortunately..), hope this helps any one in my position :)

function add_role_to_body($classes) {
    global $current_user;
    $user_role = $current_user->roles;
    return array_merge( $classes, array( $user_role[0] ) );
}
add_filter('body_class','add_role_to_body');

@MarcyCapron
Copy link

@joanna-s thank you!!

@santiazpi
Copy link

santiazpi commented Nov 12, 2019

This works for serveral user roles per user....
`function add_role_to_body($classes) {

global $current_user;
foreach ($current_user->roles as $user_role)
	$classes[] = 'role-'. $user_role;

return $classes;

}
add_filter('body_class','add_role_to_body');
`

@tedw
Copy link

tedw commented Nov 14, 2019

Here’s @santiazpi’s code with the syntax highlighting fixed, plus refactored to use a function expression as the callback:

add_filter("body_class", function($classes) {
    global $current_user;
    
    foreach ($current_user->roles as $user_role) {
        $classes[] = "role-{$user_role}";
    }

    return $classes;
});

FYI, here’s how to add role classes in the WP admin:

add_filter("admin_body_class", function($classes) {
  $user = wp_get_current_user();
  foreach ($user->roles as $user_role) {
    $classes .= " role-{$user_role}";
  }
  return $classes;
});

https://developer.wordpress.org/reference/hooks/admin_body_class/

@jancbeck
Copy link
Author

jancbeck commented Nov 15, 2019

Thanks @tedw I've updated the gist using your second function which seems to be compatible with the frontend theme as well.

@michaelignacio
Copy link

Agreed, I used the first snippet posted by @tedw and it works perfectly on the frontend. Don't really need the role as body classes on my WP Admin.

@michalprekop
Copy link

Thank you @tedw it works great!

@posgross
Copy link

HELLO
I am beginner! Pltease help!

I need the snippet, which will hide the button ADD LISTING for couple roles

Please help I don't understand

I download and install "Code Snippets"

For test I take the code from the first messages of this discussion

http://joxi.ru/Y2Lw477tMb1Rvm

I need make 2 snippets for two different roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment