How to get user roles in drupal 7.x?

15,666

Solution 1

See the user_roles() function which provides a list of all roles, or you can optionally filter on specific conditions (there are more details on the documentation page I linked to).

Solution 2

If you want to check for a specific role of the current user (in this example we use "authenticated user"):

global $user;

if (in_array('authenticated user', $user->roles)){
    //do stuff
}

Solution 3

You can use user_load() to retreive the roles for a user.

example:

$user = user_load(1);
$roles = $user->roles;
// $roles contains all the users assigned roles
Share:
15,666
Bilal
Author by

Bilal

Experienced Full Stack Web Developer adept in all stages of advanced web development. Knowledgeable in user interface, testing, and debugging processes. Bringing forth expertise in design, installation, testing and maintenance of web systems. Equipped with a diverse and promising skill-set. Proficient in an assortment of technologies, including PHP, Python, Angular, NodeJS SQL, NoSQL and AWS. Able to effectively self-manage during independent projects, as well as collaborate in a team setting. I believe in high quality work, spend time to learn and implement something new and use everything in a better and modern way. I am very strong at fast learning, research and development and starting up fast on an ongoing project.

Updated on June 04, 2022

Comments

  • Bilal
    Bilal almost 2 years

    I want to get the user roles Just like getting node types using this

    node_type_get_types();
    

    is there anyway to get user roles using a function like this?

  • Clive
    Clive over 12 years
    OP seems to want a list of all roles in the system, not all roles for a particular user