There are two WordPress functions that allow you to get a user role in WordPress in order to manage access to a page, access a
If you have the WordPress user’s id, the get_userdata WordPress function is perfect for you. However, if you have the user’s username, email, or slug, use the get_user_by WordPress function. We show you how to use both of these functions to get the current user’s roles and to identify what role(s) they’re associated with below.
USE get_userdata IF YOU HAVE USER ID:
// Get the user object.
$user = get_userdata( $user_id );
// Get all the user roles for this user as an array.
$user_roles = $user->roles;
// Check if the specified role is present in the array.
if ( in_array( 'subscriber', $user_roles, true ) ) {
// user is subscriber
} elseif ( in_array( 'administrator', $user_roles, true ) ) {
// user is an administrator
} elseif ( in_array( 'editor', $user_roles, true ) ) {
// user is an editor
} elseif ( in_array( 'author', $user_roles, true ) ) {
// user is an author
} elseif ( in_array( 'contributor', $user_roles, true ) ) {
// user is a contributor
}
You can find more information on the get_userdata function at https://codex.wordpress.org/Function_Reference/get_userdata.
USE get_user_by IF YOU DON’T HAVE USER ID:
If you need to get a user role in WordPress by the user’s email, you would have to use the get_user_by WordPress function instead.
$user = get_user_by( 'email', 'user@wplauncher.com' );
You can also get the user by their username using the get_user_by function:
$user = get_user_by('login', 'username');
And, you can get WordPress user by slug using the get_user_by function:
$user = get_user_by('slug', 'UserSlug');
For each of the get_user_by options above, you would get WordPress user roles in the same way that you did with the get_userdata function:
// Get all the user roles for this user as an array.
$user_roles = $user->roles;
And, you could use the if statements below to check on the current user’s roles (exactly as we did above):
// Check if the specified role is present in the array.
if ( in_array( 'subscriber', $user_roles, true ) ) {
// user is subscriber
} elseif ( in_array( 'administrator', $user_roles, true ) ) {
// user is an administrator
} elseif ( in_array( 'editor', $user_roles, true ) ) {
// user is an editor
} elseif ( in_array( 'author', $user_roles, true ) ) {
// user is an author
} elseif ( in_array( 'contributor', $user_roles, true ) ) {
// user is a contributor
}
Use SQL Statement to Get WordPress User Roles
The SQL statement below gets all user roles from your database. You can use a remote MySQL client like Sequel Pro or open up PHPMyAdmin and run the SQL command below in the Query or SQL field.
SELECT wp_users.ID, wp_users.user_email, wp_users.user_login, wp_usermeta.meta_value
FROM wp_users INNER JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'wp_capabilities'
The command above references the wp_users table and the wp_usermeta tables and the wp_capabilities meta_key. Make sure that if your WordPress database has a table prefix different than wp_
you replace the references to wp_users, wp_usermeta, and wp_capabilities to use that table prefix. For example, if you have a table prefix that is `wp2304_` your command will look like the following:
SELECT wp2304_users.ID, wp2304_users.user_email, wp2304_users.user_login, wp2304_usermeta.meta_value
FROM wp2304_users INNER JOIN wp2304_usermeta
ON wp2304_users.ID = wp2304_usermeta.user_id
WHERE wp2304_usermeta.meta_key = 'wp2304_capabilities'
Both of the commands above will generate a result that the user id, the user login and the meta value, which will identify the user’s role. This result looks like the following:
2 2@email.com user2 a:1:{s:13:"administrator";b:1;}
3 3@email.com user3 a:1:{s:10:"subscriber";b:1;}
4 4@email.com user4 a:1:{s:13:"administrator";b:1;}
5 5@email.com user5 a:1:{s:13:"administrator";b:1;}
6 6@email.com user6 a:1:{s:13:"administrator";b:1;}