Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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');
?>
@towfiqi
Copy link

towfiqi commented Jul 13, 2013

Thanks for the function. but instead of this:

$classes .= 'role-'. $user_role;

it should be this:

$classes[] = 'role-'. $user_role;

@werther
Copy link

werther commented Nov 6, 2013

I've used both solutions.
For me one works on frontend, the other one works on backend!?!

function class_to_body($classes) {
global $current_user;
$user_role = array_shift($current_user->roles);
$classes[] = $user_role;
return $classes;
}
function class_to_body_admin($classes) {
global $current_user;
$user_role = array_shift($current_user->roles);
$classes .= $user_role;
return $classes;
}

add_filter('body_class','class_to_body');
add_filter('admin_body_class', 'class_to_body_admin');

Someone can explain why? - WP 3.7.1

@werther
Copy link

werther commented Nov 6, 2013

The answer is in the "wp-admin/admin-heade.php" Line 154 :)

/**

  • Filter the admin CSS classes.
    *
  • This filter differs from the post_class or body_class filters in two important ways:
    1. $classes is a space-separated string of class names instead of an array.
    1. Not all core admin classes are filterable, notably: wp-admin, wp-core-ui, and no-js cannot be removed.
  • @SInCE 2.3.0
    *
  • @param string $classes Space-separated string of CSS classes.
    */

@marcochiesi
Copy link

marcochiesi commented Jul 22, 2014

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

bryanwillis commented Feb 3, 2016

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

graemebryson commented Mar 18, 2016

Cheers @bryanwillis - perfect.

@aconitewill
Copy link

aconitewill commented Jun 13, 2016

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

zebrastribe commented Aug 26, 2016

Thanx @bryanwillis.

@costindeveloper
Copy link

costindeveloper commented Aug 30, 2016

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

Garconis commented Oct 12, 2016

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

great2gether commented Mar 14, 2017

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

AzeemAnsari commented Oct 24, 2017

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

MarcyCapron commented Feb 14, 2019

@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

michaelignacio commented Jun 6, 2020

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

michalprekop commented Jan 7, 2021

Thank you @tedw it works great!

@posgross
Copy link

posgross commented Aug 26, 2021

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