What's the best way to get user roles for a given UID -- or rather, check the given UID has a particular role?

I want to check something in a theme, based on whether the node's author belongs to a role.

Comments

mdixoncm’s picture

You could do a user load to get the user object, the roles will be available in the object - for example

$account = user_load(array('uid'=>$uid));
print_r($account->roles);

But - it is generally bad form to check roles explicitly - instead you should really check for a particular permission, and then assign that permission to the role in question, this then allows much greater flexibility and makes your code more portable

$account=user_load(array('uid'=>$uid));
if (user_access('administer site configuration',$account)){
  //do your stuff here
}

The above will check to see if the user has the "administer site configuration" permission ...

Mike,
Computerminds offer Drupal development, consulting and training

ridefree’s picture

just what i was looking for ...
but not quite workin yet ...

i'm building a section of my theme (with limited php knowledge ;)

to do something like

$account=user_load(array('uid'=>$uid));
if (user_access('administer comments',$account)){
// do stuff

};

but no dice yet ...
basically just trying to show an admin area above posts in my forum.

will keep at it .... but thot i would put it here just in case