The question might be simple. Since I need to fix it soon I had to ask.
Currently I am modifying a free plugin. In that I am having the user login string.
How can I get the role for that login?
I'm using WordPress 3.8.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThe question might be simple. Since I need to fix it soon I had to ask.
Currently I am modifying a free plugin. In that I am having the user login string.
How can I get the role for that login?
I'm using WordPress 3.8.
You'll want to use get_user_by()
, which will return a WP_USER
object which contains roles and capabilities. More info here.
$user = get_user_by( 'login', 'username');
$roles = $user->roles // this will contain an array of the roles the user is in
You can use the get_user_by()
function.
Example:
Get all the roles assigned to the user John Smith
, that uses the login johnsmith
:
$user = get_user_by( 'login', 'johnsmith' );
print_r( $user->roles );
This might give you the following output:
Array ( [0] => author )