How to get current user role in WordPress
This article shows how to get the user role of the current user in WordPress from within the loop using PHP code.
WordPress is a very dynamic and flexible platform. You can use it for pretty much anything. When writing any custom code for your WordPress website, it’s important to accurately identify and authenticate the user login executing the code.
Getting the current user role in WordPress involves retrieving the role assigned to the currently logged-in user within a WordPress environment. This fundamental process is crucial for user management and access control, enabling developers to apply role-specific functionalities and permissions. Practically, one can employ built-in WordPress functions or programming code to acquire the user’s role.
WordPress offers a user account functionality with a user role interface for users to create and manage their profiles. If a user has a role of Administrator, Editor, or Author role, you can let him or her update their profile information from the WordPress dashboard. Moreover, WordPress itself defines default roles using the global variable $wp_roles. You can edit these roles by adding or removing capabilities for each role or creating a new custom user role.
WordPress offers many functions for checking the current user role of a user. Using these functions, you can easily decide what content, functionality, or actions should be available to a user in a certain user role. In this article, we’ll take a look at how you can use these functions to determine what content is available for any given user on your WordPress website.
Using these functions, you can easily decide what content, functionality, or actions should be available to a user in a certain user role.
There is no official WordPress function for giving a user a specific WordPress user role, so you need to write your own. Here’s an example custom function that you can use to check if a user can view a page or post.
You will need to add this short piece of code to the functions.php file in your active child theme, or you could use a Snippets plugin to add the code:
function my_get_current_user_roles() { if( is_user_logged_in() ) { $user = wp_get_current_user(); $roles = ( array ) $user->roles; return $roles; // This will returns an array } else { return array(); } }
In the code snippet above, we check if the user is logged in. Then, we use the wp_get_current_user to return the WP_User object of the current user. After that, we can access the user role(s) via $user->roles. We return the roles in the same format as specified above (as an array). This is because a user can have more than one WordPress user role, including a custom user role.
You need to add two lines of code to the functions.php file of your theme, and you can echo the current user role of the logged-in user so you can display it anywhere in your theme. If you want to test the code, you can hook it to the wp_header and print it out to test if everything works properly:
add_action( 'wp_head', 'my_get_current_user_roles'); function my_get_current_user_roles() { if( is_user_logged_in() ) { $user = wp_get_current_user(); $roles = ( array ) $user->roles; //return $roles; // This will returns an array return array_values($roles); } else { return array(); } }
Obtaining the current user’s role in WordPress programmatically serves a variety of essential purposes:
- Conditional Display: Customize content or features based on the role-based data for plugins or services.
- Role-Based Access Control: Get current user role to regulate permissions and restrict access to certain areas.
- User Experience Personalization: Tailor content and functionality for a role-specific experience.
- Content Filtering: Modify content presentation or behavior dynamically.
- User Metrics and Insights: Analyze user behavior for engagement optimization based on multiple roles.
- User Management: Simplify administrative tasks, such as registration and editing, based on user roles and capabilities.
- Security: Verify roles for sensitive actions, enhancing site security.
- Logging and Auditing: Record and track user activities and role-related changes for auditing.
By programmatically accessing the current user’s role, developers can effectively control the display of content, user capabilities, or feature accessibility, aligning their WordPress websites with distinct and user-specific requirements.
Conclusion
WordPress role and capabilities are an important part of every WordPress site as they allow us to securely control access to content, website functionality, and access to custom capability, depending on the user type. In this short tutorial, we showed you how to get the user role of the current user in WordPress programmatically.