If 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.
Table of Contents
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:
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.

Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
More articles written by Joe
Similar Articles
- WooCommerce Redirect After Logout [Ultimate Guide]
- How to Remove or Disable Reviews In WooCommerce
- Complete Guide to Using WordPress Custom Hooks do_action & apply_filters With Examples
- How to Create Number Pagination in WordPress Without Using Plugin
- How to Create WordPress Custom User Roles
- How to Hide Category From Shop Page WooCommerce
- How to Create Shortcode for Plugin in WordPress
- How to Add Product to Cart Programmatically in WooCommerce
- How to Add Search Box on Top of Page Storefront Theme
- How to Get Logged In User Information in WordPress
- How to Check if User is Logged In WordPress
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Hide Admin Bar for WooCommerce Customers or By User Roles
- WooCommerce Redirect Users After Registration by Roles
- How to Move Description Under Image in WooCommerce
- How to Get Current Product Category Name in WooCommerce
- How to Create WooCommerce Admin Notice for Plugin – WooCommerce Development
- How to Hide Tax Label In WooCommerce
- How to Customize Product Category Page In WooCommerce
- How to Create Product Programmatically WooCommerce
Comments are closed.