How to find out the role of the current user in WordPress?

While the user is creating a new message, how to determine his current role?

+7
wordpress custom-post-type
source share
2 answers

I assume that you know which Wordpress interceptors you want to use. Therefore, skipping this part, it is quite easy to get the current user role.

$current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; $roles = $current_user->roles; //$roles is an array 

Now you can iterate over this array to find out if the user has a specific role.

Or you can use current_user_can to search for specific features if you just want to check if the user has a specific permission or not in the role. For example:

 if (current_user_can('delete_posts')) { //display the delete posts button. } 
+9
source share

This code will help you

 <?php echo array_shift(wp_get_current_user()->roles); ?> 
0
source share

All Articles