Steps
-
Add the PHP code to your theme’s
functions.phpfile or a PHP snippets plugin. -
Save and activate the snippet.
-
Log in with different WordPress user roles.
-
Inspect the page’s HTML body class.
/**
* Add user role as a body class
*/
function custom_add_user_role_body_class( $classes ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
foreach ( $user->roles as $role ) {
$classes[] = 'user-role-' . sanitize_html_class( $role );
}
}
return $classes;
}
add_filter( 'body_class', 'custom_add_user_role_body_class' );
Tested on
WordPress 6.8
Reported by the author on November 3, 2026. WPFolks has not verified this.
Not tested against WordPress 7.1.4. It may still work.
