How to get a users Roles and how to list all WordPress roles in PHP

This code is handy if you want to find out a users role/s. You could expand it and check if a user has a given role or similar.

This code is based on examples from:

https://codex.wordpress.org/Function_Reference/get_userdata

and

https://wordpress.stackexchange.com/questions/1665/getting-a-list-of-currently-available-roles-on-a-wordpress-site

If you did want to check if the user is in a certain group you could do something like this:

 

1 thought on “How to get a users Roles and how to list all WordPress roles in PHP”

  1. Nice! Can you suggest a way to find the highest level role for a single user? Many times users have multiple roles. I am looking to find and return the highest access, such as editor or administrator.

    Reply

Leave a Comment