Skip to content

Instantly share code, notes, and snippets.

@grayayer
Last active April 22, 2017 01:23
Show Gist options
  • Save grayayer/7121b32f0e31ac6528f2bb6c45e200ba to your computer and use it in GitHub Desktop.
Save grayayer/7121b32f0e31ac6528f2bb6c45e200ba to your computer and use it in GitHub Desktop.
Appends user role and id to the body classes for for front and backend in wordpress. Place this in a functionality plugin or functions.php file in the theme.
function add_helpful_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', 'add_helpful_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 to front-end body tag
// 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 body class array */
$user_ID = $current_user->ID;
$classes .= $user_role.' '.'user-id-'.$user_ID ;
return $classes;
return 'user-id-'.$user_ID;
}
@grayayer
Copy link
Author

This goes well for the backend with adding custom css to the backend:
https://css-tricks.com/snippets/wordpress/apply-custom-css-to-admin-area/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment