Add User Role as Body Class with WordPress Functions


Build your own WordPress Support Plan

Expert, yet affordable WordPress support & maintenance plans.
View our pricing

This guide explains how to easily add the current user role as a class to the WordPress body in the front-end and the WP admin, this would look like this so class="administrator user-id-1". This is particularly useful if you want to add a style for a specific user or user role group. It can also be used to hide elements quickly although we don’t recommend this method for hiding elements with CSS.

The function below can be used in the theme functions or better in an MU plugin, if you are using a Multisite then we highly recommend that you place it in an MU plugin.

 * Add User Role Class to Body
 * Referenced code from
function print_user_classes() {
    if ( is_user_logged_in() ) {
        add_filter('admin_body_class', 'class_to_body_admin');
add_action('init', 'print_user_classes');

/// Add user role class to front-end body tag
function class_to_body($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    $classes[] = $user_role.' ';
    return $classes;

/// Add user role class and user id to front-end body tag

// add 'class-name' to the $classes array
function class_to_body_admin($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    /* Adds the user id to the admin body class array */
    $user_ID = $current_user->ID;
    $classes = $user_role.' '.'user-id-'.$user_ID ;
    return $classes;
    return 'user-id-'.$user_ID;

If you need help adding this function to your site please leave a comment below, need to do something more complex? Then get in touch with us, our One-Off WordPress Support is as little as $20.


We can add user role n <body> tag class using the above method.

Always make sure to keep your WordPress core, plugins, and themes up to date, as well as maintain strong password policies, to enhance the overall security of your website further. Additionally, consider other security measures like using a reputable web hosting provider and regular backups.

For more information on WordPress basic settings, maintenance routines, or hiring support services for WordPress, let us know your thoughts on custom plans, deals, and support services.

Written By
Founder of WP Helper.
You will also like these articles

Relax Knowing Your WordPress Site Is Secure & Running Smoothly 24/7

Let us manage your WordPress site, everything from security to updates will be taken care of. Support plans also come with dedicated support so we can do anything from adding content to customising your site for you.

Same Day Professional WordPress Support

Get WordPress Support Today

Need help with a single WordPress problem, today? We can help with anything from adding analytics tracking code to site hack recovery. Full money back guarantee on all jobs.

1. Submit a Support Request

Use our support ticket form below to send details of your problem to our developers.

2. Get a Quote

We will review your request and provide a quote within 24 hours (but usually within a few hours).

3. We fix your WordPress problem

Our team will begin fixing your WordPress problem the same day.

4. 100% Money Back Guarantee

If we can’t fix the problem for the amount quoted we will refund you 100%.

  • Do you have a screenshot of the issue or have a copy of the theme or plugin that is at fault? If you want to upload php, html or css please zip first.
    Drop files here or
    Accepted file types: jpg, jpeg, png, pdf, zip, gzip, rar, doc, txt, Max. file size: 15 MB, Max. files: 10.