Add User Role to Body Class

I’ve worked with a few membership-based websites, and found this method to be the easiest way to ensure layout customization for different user types:

add_filter('body_class','add_role_to_body');
function add_role_to_body($classes) {
    $current_user = new WP_User(get_current_user_id());
    $user_role = array_shift($current_user->roles);
    $classes[] = 'role-'. $user_role; // change prefix as desired
    return $classes;
}

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.