Sometimes you want to add a body tag for a specific user role. This as you want to hide or show specific parts of a page, or site for a particular user. Here is how you can do just that.

add_filter( 'body_class', 'imwz_role__body_class' );
function imwz_role__body_class( $classes ) {
 $user = wp_get_current_user(); 
 if ( in_array( 'subscriber', $user->roles ) ) {     
     $classes[] = 'class-name'; // your custom class name 
  } 
  return $classes;
 }

As you can see the body_class filter is used. Inside it we get the user role properly using wp_get_current_user and an array checking the roles.

Once done you could see something like this for classes

product-template-default single single-product postid-729 logged-in admin-bar theme-Divi et-tb-has-template et-tb-has-body admin-role woocommerce woocommerce-page woocommerce-js et_bloom et_button_custom_icon et_pb_button_helper_class et_fixed_nav et_show_nav et_primary_nav_dropdown_animation_fade et_secondary_nav_dropdown_animation_fade et_header_style_left et_pb_footer_columns_1_4__1_2 et_cover_background et_pb_gutter osx et_pb_gutters3 et_divi_theme et-db et_minified_js et_minified_css customize-support chrome

waarin class admin-role op basis administrator filter is toegevoegd.

Props Mukto90 https://wordpress.stackexchange.com/a/268178/12260

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *