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) {
global $current_user;
$user_role = array_shift($current_user->roles);
$classes .= 'role-'. $user_role;
return $classes;
}
add_filter('body_class','add_role_to_body');
add_filter('admin_body_class', 'add_role_to_body');
?>
@towfiqi

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 9, 2016

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

@gega23

This comment has been minimized.

Copy link

commented Feb 10, 2016

Me too! Saved my day. Thanks @bryanwillis

@graemebryson

This comment has been minimized.

Copy link

commented Mar 18, 2016

Cheers @bryanwillis - perfect.

@aconitewill

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 26, 2016

Thanx @bryanwillis.

@costindeveloper

This comment has been minimized.

Copy link

commented Aug 30, 2016

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

@vithefiddler

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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!

@beingazim

This comment has been minimized.

Copy link

commented Oct 24, 2017

where to add this code guys?

@joanna-s

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 14, 2019

@joanna-s thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.