[wp-hackers] User Roles

Andy Staines andy at yellowswordfish.com
Mon Mar 19 16:01:49 GMT 2007


> Andy Staines wrote:
>> I'm pretty sure I must have missed the obvious on my trawl through 
>> the code but can someone tell me the 'correct' way to get the 
>> currently logged in users 'role'. Don't need the capabilities or 
>> levels - just the role. The methods I am using seem somewhat 
>> long-winded...
>
> Jennifer Hodgdon wrote:
> I believe that as of 2.0, users can be assigned to multiple roles (by 
> plugins, if not through the user interface), and I think they may be 
> able to be assigned capabilities outside of roles as well. So it might 
> not make much sense to ask what the current user's role is.
>
> Assuming you are writing a plugin, the right thing to do (as I 
> understand it) is to define a capability for your plugin (or use one 
> of the built-in capabilities) and ask whether the current user has 
> permission to use that capability with the current_user_can function 
> (in wp-includes/capabilities.php).
>
Thanks Jennifer but I am not actually after assigning or retrieving 
capabilities. And if I get multiple roles then that too is fine 
(although as you say this does not seem possible in standard WP). I need 
the role(s) as items in my plugin  can be assigned to roles (not levels 
and not capabilities) including user-defined roles.
 
I am currently making a call to wp_get_current_user() and peeling the 
role from there but it seems like an overloaded, top-heavy structure to 
go and get and I was simply hoping that there might be a simpler built 
in call that I hadn't stumbled across. I have also had one or two users 
who have reported that the role returned from this has been empty 
although this might be something I am doing incorrectly (investigating 
that).

Actually I am keen to get the 'highest level' assigned to a role as well 
(which is easy enough - no problem there) but if there is an inexpensive 
way of getting that I'd also like to know. The old wp_user_level in 
usemeta can not be relied upon to be accurate if some 'role' plugins are 
in use.

So - is there an improvement on using wp_get_current_user() ?
Thanks
Andy







More information about the wp-hackers mailing list