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 management interface, allowing users to create and manage their profiles. Administrators, Editors, or Authors can modify profile data directly from the dashboard. Beyond the default user roles defined by the global variable $wp_roles, you can customize these roles by adding or removing specific capabilities or even creating new user roles. To further enhance user role management, you can utilize plugins like Users Insights, which offers advanced filtering and custom fields capabilities. This allows administrators to gain deeper insights into user roles and manage them efficiently with smart filters and detailed user profiles, streamlining tasks like access control and role-based content delivery​​.

wordpress get current 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:

  1. Conditional Display: Customize content or features based on the role-based data for plugins or services.
  2. Role-Based Access Control: Get current user role to regulate permissions and restrict access to certain areas.
  3. User Experience Personalization: Tailor content and functionality for a role-specific experience.
  4. Content Filtering: Modify content presentation or behavior dynamically.
  5. User Metrics and Insights: Analyze user behavior for engagement optimization based on multiple roles.
  6. User Management: Simplify administrative tasks, such as registration and editing, based on user roles and capabilities.
  7. Security: Verify roles for sensitive actions, enhancing site security.
  8. 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.

While manually retrieving user roles is valuable, using tools like Users Insights elevates this process. This plugin seamlessly integrates with WordPress, providing powerful user management features like smart filters, custom user fields, and user activity tracking. For example, if you need to filter users by their role (Administrator, Subscriber, etc.) or by custom roles, Users Insights lets you do this effortlessly with its stackable filters.

Additionally, the Geolocation and Page Visit Tracking features enable you to understand user behavior based on their role and location, giving you actionable insights to enhance content delivery and security​​.

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.