wordpress: add user role on body class

I’m trying to add the user role once a user is logged in on wordpress and have the user role be in the body class=””

function my_class_names($classes) {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

    if  ( is_user_logged_in() )  {
        $classes[] = $user_role;
    }
return $classes; 
} 
add_filter('body_class','my_class_names');

I’m not a php guy already tried research and i found how to get the user role, and another post was how to add body class and i’m have trouble how to get them both to work. I hope someone can help me out with this spent more than 2hrs trying to figure this out by myself 🙁

Related posts

Leave a Reply

5 comments

  1. 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;
    return $classes;
    }
    

    In my Case this code works perfectly by adding user role class to body class 🙂

    Simply copy the code to function.php and paste it in a perfect place 😉

  2. You can change your function to this

    function get_user_role() {
        global $current_user;
        $user_roles = $current_user->roles;
        $user_role = array_shift($user_roles);
        return $user_role;
    }
    

    Then, change your body tag like this

    <body <?php body_class( get_user_role() ); ?>>
    

    If the user isn’t logged in, it will not add anything.

    Or you can also add one more function (from the codex)

    add_filter('body_class','my_class_names');
    function my_class_names($classes) {
        $classes[] = get_user_role();
        return $classes;
    }
    

    That will work.

  3. WordPress 5.8 Answer:

    You can easily add the current user’s role as a class to the body tag by adding the following code to your functions.php file in your theme.

    function add_user_role_to_body_classes( $classes ) {
        global $current_user;
    
        foreach ( $current_user->roles as $user_role ) {
            if ( is_admin() ) {
                $classes .= ' role-' . $user_role;
            } else {
                $classes[] = 'role-' . $user_role;
            }
        }
    
        return $classes;
    }
    add_filter( 'body_class', 'add_user_role_to_body_classes', 100 );
    add_filter( 'admin_body_class', 'add_user_role_to_body_classes', 100 );
    

    Explanation of what this code does

    It retrieves the roles that this user is a part of, and adds each one to the body class. And since WordPress supports assigning multiple roles to a user, this function will print out all roles a user is a part of. Each class will be prefixed with role-, so a user with the Editor role would output role-editor to the body.

    The add_filter( 'body_class', 'add_user_role_to_body_classes', 100 ); line adds the user role class(es) to the body tag on the front-end.

    The add_filter( 'admin_body_class', 'add_user_role_to_body_classes', 100 ); line adds the user role class(es) to the body tag on the back-end (admin side of WordPress).

  4. You can add to the front or to the backend like so;

        /* Adds the user id to the admin body class array
          * Add User Role Class to Body
          * Ref https://lakewood.media/add-user-role-id-body-class-wordpress/
        */
    
    
        function print_user_classes() {
         if ( is_user_logged_in() ) {
          add_filter('body_class','class_to_body');
          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 
      /// 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 / backend body class array */
           $user_ID = $current_user->ID;
           $classes = $user_role.' '.'user-id-'.$user_ID ;
          return $classes;
         return 'user-id-'.$user_ID;
        }
    
  5. Similarly, if you wanted to add the user-id to the body class in the backend, you’d use:

    /* Adds the user id to the admin body class array */
    add_filter('admin_body_class', 'add_username_to_body');
    function add_username_to_body( $classes ) {
        // add 'class-name' to the $classes array
        global $current_user;
        $user_ID = $current_user->ID;
        $classes[] = 'user-id-' . $user_ID;
        // return the $classes array
        return 'user-id-'.$user_ID;
    }