Take the current user's wordpress role, and add it to the body class as role-[users role]
Last active
May 19, 2023 14:16
-
-
Save zackpyle/1b9808cf353826d8ce9dd6c0ac4785ac to your computer and use it in GitHub Desktop.
Add WP user role as body class #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this line - only for styling | |
// Add WP user role to backend body class | |
add_filter('admin_body_class', 'add_role_to_admin_body'); | |
function add_role_to_admin_body($classes) { | |
global $current_user; | |
$user = clone $current_user; //clone global var - we don't want to change it | |
$user_role = array_shift($user->roles); | |
$classes = 'role-'. $user_role; | |
return $classes; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this line - only for styling | |
// Add WP user role to frontend body class | |
add_filter('body_class','add_role_to_body'); | |
function add_role_to_body($classes) { | |
if ( is_user_logged_in() ) { | |
$current_user = new WP_User(get_current_user_id()); | |
$user_role = array_shift($current_user->roles); | |
$classes[] = 'role-'. $user_role; | |
} | |
return $classes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment