Skip to content

Instantly share code, notes, and snippets.

@dustinleer
Last active June 10, 2022 18:19
Show Gist options
  • Save dustinleer/757d4426342d397cc8376fa5f0d2869c to your computer and use it in GitHub Desktop.
Save dustinleer/757d4426342d397cc8376fa5f0d2869c to your computer and use it in GitHub Desktop.
Add User Role Class to Body
<?php
/**
* Add User Role Class to Body
*/
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_' . $user_role . ' ';
return $classes;
}
// 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_role. ' ' . 'user-id-' . $user_ID ;
return $classes;
return 'user-id-' . $user_ID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment