Useful Snippets

Useful Snippets


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

August 2020
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Categories


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

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

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.