Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active December 23, 2019 18:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kellenmace/ff068164a6d0aaf23679dbcb7698fff0 to your computer and use it in GitHub Desktop.
Save kellenmace/ff068164a6d0aaf23679dbcb7698fff0 to your computer and use it in GitHub Desktop.
Get user role in WordPress
<?php
/*
* Get user's role.
*
* If $user parameter is not provided, returns the current user's role.
* Only returns the user's first role, even if they have more than one.
*
* @param mixed $user User ID or object. Pass nothing for current user.
*
* @return string The User's role, or an empty string if none.
*/
function km_get_user_role( $user = null ) {
$user = $user ? new WP_User( $user ) : wp_get_current_user();
return $user->roles ? $user->roles[0] : '';
}
@cowgill
Copy link

cowgill commented Jul 5, 2017

Very handy. Thanks!

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