Check A User’s Role in WordPress
You can easily check a user’s capability in WordPress using the current_user_can
function. Unfortunately this breaks sometimes when trying to check roles. For example, you cannot use that function to check if a user’s role is equivalent to or greater than a subscriber.
The function below will allow you to check a user’s role against a single or multiple roles. Just pass the roles to check against as an array. This function will always check if a user has the administrator role even if it is not passed as a role.
To check if the current user has the editor and author roles you would do the following:
// Define roles to check $roles = array('editor','author'); // Check roles $in_role = check_user_role($roles); // Do something based on role if ($in_role) { // User in role, do something } else { // User not in role do something else }