Skip to content

Instantly share code, notes, and snippets.

@tobedoit
Created November 26, 2012 07:01
Show Gist options
  • Save tobedoit/4146948 to your computer and use it in GitHub Desktop.
Save tobedoit/4146948 to your computer and use it in GitHub Desktop.
Wordpress: display current user role
/* 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;
}
@dJunkyKode
Copy link

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?

@madanpraba
Copy link

Thank you @tobedoit

@sergiofh2s
Copy link

sergiofh2s commented May 14, 2022

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

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