-
-
Save tobedoit/4146948 to your computer and use it in GitHub Desktop.
/* Display Current User Role *************************************************************************** */ | |
/* http://wordpress.org/support/topic/how-to-get-the-current-logged-in-users-role ********************** */ | |
/* !로그인 회원역할 표시 *********************************************************************************** */ | |
/* <?php echo get_current_user_role(); ?> in your template ******************************************** */ | |
function get_current_user_role() { | |
global $wp_roles; | |
$current_user = wp_get_current_user(); | |
$roles = $current_user->roles; | |
$role = array_shift($roles); | |
return isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : false; | |
} |
@maglyen did you figured it out? , i need the same code
function get_current_user_role() {
global $wp_roles;
$current_user = wp_get_current_user();
$roles = $current_user->roles;
return $roles? $roles : null; // returns roles if any found, else returns null
}
Thank you so much for your contribution @RavingKoala,
Say I would like to display the roles of the users on their "Account Page" a page in the front end, maybe in a widget area or even inside the page. Something like:
"Welcome Mate"
You are currently a
- Subscriber
- Contributor
- ect.
Would I need to insert a short code or something like that?
Thank you @tobedoit
function get_all_current_user_rolles() {
global $wp_roles;
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
echo '<ul>';
foreach( $roles as $role ){
echo '<li>' . $wp_roles->roles[ $role ]['name'] . '</li';
}
echo '</ul';
}
}
add_shortcode( 'get_all_currentuserrole_shortcode', 'get_all_current_user_rolles');`
@dJunkyKode Almost five years later but it works for me.
thanks you @tobedoit
Is there a way to modify this code to display all of a user's roles? For example, the user has a primary role of Customer and a secondary role as Subscriber. Is there a way to show both roles at once?