-
-
Save jancbeck/3178689 to your computer and use it in GitHub Desktop.
<?php | |
// Add role class to body | |
function add_role_to_body($classes) { | |
foreach (wp_get_current_user()->roles as $user_role) { | |
$classes[] = 'role-'. $user_role; | |
} | |
return $classes; | |
}); | |
add_filter('body_class','add_role_to_body'); | |
add_filter('admin_body_class', 'add_role_to_body'); | |
?> |
This works for serveral user roles per user....
`function add_role_to_body($classes) {
global $current_user;
foreach ($current_user->roles as $user_role)
$classes[] = 'role-'. $user_role;
return $classes;
}
add_filter('body_class','add_role_to_body');
`
Here’s @santiazpi’s code with the syntax highlighting fixed, plus refactored to use a function expression as the callback:
add_filter("body_class", function($classes) {
global $current_user;
foreach ($current_user->roles as $user_role) {
$classes[] = "role-{$user_role}";
}
return $classes;
});
FYI, here’s how to add role classes in the WP admin:
add_filter("admin_body_class", function($classes) {
$user = wp_get_current_user();
foreach ($user->roles as $user_role) {
$classes .= " role-{$user_role}";
}
return $classes;
});
https://developer.wordpress.org/reference/hooks/admin_body_class/
Thanks @tedw I've updated the gist using your second function which seems to be compatible with the frontend theme as well.
Agreed, I used the first snippet posted by @tedw and it works perfectly on the frontend. Don't really need the role as body classes on my WP Admin.
Thank you @tedw it works great!
HELLO
I am beginner! Pltease help!
I need the snippet, which will hide the button ADD LISTING for couple roles
Please help I don't understand
I download and install "Code Snippets"
For test I take the code from the first messages of this discussion
I need make 2 snippets for two different roles
@joanna-s thank you!!