Created
July 31, 2018 08:37
-
-
Save mahbubme/05f0a7594531a94732c07e53ccc9218e to your computer and use it in GitHub Desktop.
Add user role name to body tag as class
This file contains hidden or 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 | |
// Add user role as body class | |
function print_user_classes() { | |
add_filter('body_class','class_to_body'); | |
} | |
add_action('init', 'print_user_classes'); | |
// Add user role class to front-end body tag | |
function class_to_body($classes) { | |
if ( is_user_logged_in() ) { | |
global $current_user; | |
$user_role = array_shift($current_user->roles); | |
$classes[] = $user_role . ' '; | |
} else{ | |
$classes[] = 'loggedout-product-class'; | |
} | |
return $classes; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment