Why does admin_body_class not work?

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 users 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;
});

Topic body-class user-roles filters Wordpress

Category Web


This works for the admin backend; I needed to restructure the filter to be able to add a priority:

function add_admin_body_class($classes) {
$user = wp_get_current_user();
foreach ($user->roles as $user_role) {
    $classes .= " role-{$user_role}";
  }
  return $classes;
}
add_filter("admin_body_class", "add_admin_body_class", 9999);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.