While a user is creating a new post, how do I determine his current role?
+1
A:
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 the easier function current_user_can.
if (current_user_can('administrator')) {
//do stuff for administrator roles
}
current_user_can can also be used 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.
}
villecoder
2010-08-17 03:59:03
Great! This is just what I'm looking for! :) Thanks! By the way, which hook do i have to use for this code?
Giljed Jowes
2010-08-17 05:51:56
I would hook save_post
villecoder
2010-08-17 12:40:13