Add user role to admin body classes in WordPress

Code:

add_filter('admin_body_class', function($classes) {
    global $current_user;

    if(is_array($current_user->roles)) {
        foreach($current_user->roles as $role) {
            $classes .= "user-role-{$role} ";
        }
    }

    return rtrim($classes);
});

Classes will look like this:

<body class="... user-role-administrator ...">

2 thoughts on “Add user role to admin body classes in WordPress

  1. John

    Hello!

    It only work when editing a post.
    How to put this class in entire admin area?

    Cheers

    Reply

Leave a Reply

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

Markdown is allowed in comments.