Check if user has certain role


Get raw version
  1. if (array_intersect(array('administrator','editor'), array_values($user->roles))) {
  2. $access = TRUE;
  3. }


jrohit's picture

First, you have to define $user object and then load user.

  1. global $user;
  2. $account = user_load($user->uid);
  3. if (array_intersect(array('administrator','editor'), array_values($account->roles))) {
  4. $access = TRUE;
  5. }
pari's picture

In Drupal 7.36 was added new function user_has_role() which may be better in certain scenarios

jrohit's picture

we have to load the user object anyway.