I'm trying to add the body class of the current user role to both the frontend and the admin backend.
Why does admin_body_class
not work for admin?
The comment to this question does not work: How to insert the current user's role into the body class in the admin backend
This works for the front end in the theme's functions.php file:
add_filter("body_class", function($classes) {
global $current_user;
foreach ($current_user->roles as $user_role) {
$classes[] = "role-{$user_role}";
}
return $classes;
});
This does not work for the backend in the theme's functions.php file:
add_filter("admin_body_class", function($classes) {
$user = wp_get_current_user();
foreach ($user->roles as $user_role) {
$classes .= " role-{$user_role}";
}
return $classes;
});
admin_body_class
filter could be fired again after yours, and your changes could be overwritten. I can't say if this is the case, but I'd try adding a high priority value.