There’s no WordPress function to directly get the current user’s role, so I typically include my own function to serve that purpose, similar to the one below. This function is even more versatile since if you pass a specific user’s ID or User object to it, it will return that user’s role. If you pass nothing to it, you’ll get the role of the current user instead.

Note that if a user has multiple roles assigned to them, this function will only return the first role.

6 comments

  1. Paul March 31, 2016 at 8:39 am

    Reply

    Hello! Thanks for code. It works like charm. But I found some little mistake. In line 13 You must to put )

    • Kellen Mace April 1, 2016 at 9:30 am

      Reply

      Paul- thanks for the heads up! I fixed that mistake, and also refactored the function a bit to account for when get_userdata() is unable to find the user.

  2. Pedro Lima April 23, 2016 at 11:14 am

    Reply

    Works like a charm! Thank you for this simple but very useful piece of code. 🙂

  3. Adrian Monecchi December 25, 2016 at 5:53 pm

    Reply

    Nice approach, it really helped me to inspect the current user’s role in the frontend. Thanks for the snippet!

  4. 083n February 26, 2017 at 10:34 am

    Reply

    Hi,

    Can we use this with conditional logic for user roles based hooks ?
    Like this one : http://wordpress.stackexchange.com/questions/258031/buddypress-user-profile-menu

    • Kellen Mace March 4, 2017 at 11:24 pm

      Reply

      Yes you can. Although it looks like the bp_core_remove_nav_item() function in that example only works for the current user, so you wouldn’t want to get the role of a non-logged in user and use that BP function and expect it to work.

Leave a reply

Your email address will not be published. Required fields are marked *