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.

12 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.

  5. Rachel May 19, 2017 at 4:29 pm

    Reply

    Nice! This was super helpful. Thanks!

  6. LI BING ZHAO June 10, 2017 at 3:00 pm

    Reply

    Thank u

  7. Thiagoveklas June 14, 2017 at 2:42 pm

    Reply

    Muito levei um tempo procurando , salvou muitas horas de trabalho.
    Muito obrigado.

  8. zx January 2, 2018 at 7:08 am

    Reply

    Very helpfull

  9. tamim February 22, 2018 at 1:23 am

    Reply

    “`
    return $user->roles ? $user->roles[0] : false;
    “`
    This code is highly buggy! If you change a user roles by editing manually than $user->roles key get changed. So, proper way to get user roles is this way

    “`
    return $user->roles ? array_shift( $user->roles ) : false ;
    “`

    • Kellen Mace February 22, 2018 at 8:06 am

      Reply

      This is incorrect. For example, if a WP_User object contains a ‘roles’ array with these roles:

      array(
      [0] => 'administrator',
      [1] => 'editor'
      );

      then you remove the first one using $user->remove_role( ‘administrator’ ), the array becomes:

      array(
      [0] => 'editor'
      );

      As long as you’re using the proper methods for adding/removing roles, the WP_User object is guaranteed to always have a role at array index [0], unless the array is empty. So in my code, the $user->roles ? check ensures that we’re dealing with a truthy (non-empty) array before proceeding to return the value of the first element in that array. If the array is empty, we return false instead.

      Furthermore, in cases when you want to get the value of the first element in an array and aren’t sure what its index/key is, I would not use array_shift(), since that is a mutating operation that permanently modifies the original array, removing its first element. I would do this instead:

      $array = array(
      'apple' => 'red',
      'orange' => 'orange',
      'banana' => 'yellow
      );
      $array_reindexed = array_values( $array );
      $first_element = $array_reindexed[0]; // This will be set to 'red'.

      This approach favors data immutability and can help prevent bugs, since the arrays always have the same values from the time they’re set through to the end of the function, rather than their values changing part way through the code.

Leave a reply

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