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!

RSS Latest posts from my personal blog


Most viewed posts


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

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.