How to know user role capabilities in WordPress?


In WordPress, you can know a user’s role and capabilities in several ways:

  1. User Profile: You can view a user’s role and capabilities by going to Users > All Users in the WordPress admin dashboard. Click on the user you want to check, and you will see their User Profile. Here, you can see their Role and capabilities listed under the “Capabilities” section.
  2. User Role Editor Plugin: You can install and activate the User Role Editor plugin, which will allow you to view and modify the capabilities of each user role. Once activated, go to Users > User Role Editor, and you will see a list of user roles. Click on any role to see its capabilities.
  3. Code: You can use code to display a user’s role and capabilities. For example, the following code will display the current user’s role:
[php]
$current_user = wp_get_current_user();
echo 'User role: ' . $current_user->roles[0];
[/php]

Similarly, you can use the current_user_can() function to check if a user has a specific

capability. For example:

[php]
if ( current_user_can( 'edit_posts' ) ) {
  // User has the capability to edit posts.
}
[/php]

Note that user capabilities are determined by their role and any additional capabilities granted to them through plugins or custom code.

Published by PHP Technology Tutorials

PHP Developer

Leave a comment