Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Add user role to admin body classes in WordPress

Stanislav KhromovStanislav Khromov

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 ...">
PHP

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 2
  • John
    Posted on

    John John

    Reply Author

    Hello!

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

    Cheers


  • iLen
    Posted on

    iLen iLen

    Reply Author

    Thank you, it was very useful.