Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the role of current user in WordPress?

While a user is creating a new post, how do I determine his current role?

like image 971
Giljed Jowes Avatar asked Aug 17 '10 03:08

Giljed Jowes


1 Answers

I'm assuming you know what hooks of Wordpress you want to use. So skipping that part, it's pretty easy to get the current role of the user

$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 that array to see if the user has a particular role.

Or, you can use current_user_can to look for specific capabilities, if you just want to check whether or not a user has a specific permission versus whether or not they're in the role. For example:

if (current_user_can('delete_posts')) {
  //display the delete posts button.
}
like image 106
villecoder Avatar answered Oct 05 '22 05:10

villecoder