How to Get Current User Role in WordPress & Display Roles

how to get current user role in WordPressIf you are writing a WordPress plugin and you want to get current user role in WordPress there are several ways you get the user roles in WordPress. In this post, I want to focus on how you can get the currently logged user and the user role as you will see in the explanation.  Previously, I wrote on how to create WordPress custom user roles and also how to redirect users after registration in WooCommerce by the roles.

WordPress User Roles

WordPress user roles play a critical role when you are developing a theme or a WordPress plugin that controls the content or requires different permissions or capabilities for each role.

By default WordPress user roles come with an array of capabilities, you can learn more about the basics WordPress user roles before you begin creating plugins or themes that require you to get current user role for different purposes.

Get Current User Role in WordPress

To get the current user role in WordPress you need first to check if the user is logged in then use the wp_get_current_user() function to retrieve the data about the roles. If you don’t know how to know if a user is logged in WordPress, I wrote a good tutorial here – how to check if the user is logged in in WordPress.  

WP Function: wp_get_current_user()

This is a WordPress function that does the magic of pulling all the data about the user who is logged in and returns the WP_user object that we can loop through to pick what we want. In this case, we will return the roles.

This function retrieves the current user object and if the user is not set it will set the current user as the logged-in person. This is why; I mentioned above, you need to first check if the user is logged in as a validation.

Get Current User Role Code Snippet

Depending on how you want to use this code, you can add an action hook or use it within a class in your plugin development.  The function to get the current user role is as follows:

add_action( 'wp_head', 'njengah_get_current_user_role'); 

function njengah_get_current_user_role() {
	
 if( is_user_logged_in() ) { // check if there is a logged in user 
	 
	 $user = wp_get_current_user(); // getting & setting the current user 
	 $roles = ( array ) $user->roles; // obtaining the role 
	 
		return $roles; // return the role for the current user 
	 
	 } else {
		 
		return array(); // if there is no logged in user return empty array  
	 
	 }
}

You can add this code to the functions.php file for testing and I have added an action hook to the header –  wp_header and a print_r to display the data on the header as shown on the image below:

Get Current User Role in WordPress

Current User Multiple Roles

As you can see on that image the data shows that the current user has two roles the administrator role and the BuddyPress - bbp_keymaster role.  Essentially, $user->roles returns all the roles of the current user.

Final Thoughts

In this post, I shared how you can get the current user roles In WordPress and use them in your theme or plugin development. To understand better the way WordPress user roles works, you may find my tutorial on how to create custom user roles a good place to start and the _wp_get_current_user() function to be resourceful.

This function entails how the wp_get_current_user() works.  Understanding user roles can also be useful for WooCommerce developer who wants to change the way users are handled in different WooCommerce events. A good example is this tutorial on how to redirect WooCommerce users based on User role.

Similar Articles

  1. WooCommerce Redirect After Logout [Ultimate Guide]

Comments are closed.