Add current user role as a class to WordPress admin element

WordPress
// PHP

What it does

Append the current logged-in user’s role to the Body element in the WordPress admin area.

The Code

function guyPrimaveraAddRoleClass($classes) {
    global $current_user;                                   // Get the current logged-in user
    $user_role = array_shift($current_user->roles);         // Store the user's role as a variable
    $classes .= 'role-'. $user_role;                        // Append this role to the variable $classes
    return $classes;
}

add_filter('admin_body_class', 'guyPrimaveraAddRoleClass'); // Add this class to the body element in the admin area

How To Use It

Paste this snippet into your theme's functions.php or in a WordPress plugin.

Menu