Support » Plugins and Hacks » Getting a WP user’s role

  • Resolved ninnypants

    (@ninnypants)


    I need to retrieve a users role for some functionality in my site, but cannot find a way to get the role.
    I’ve tried get_currentuserinfo() but have had no luck with that. Is there another way to retrieve this information?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Something like…

    <?php if (current_user_can('****_****')){ ?>

    …followed by conditional statements maybe? I’m not sure if that’s on the right track or not (’cause I’m just a hack)!

    🙂

    list of capabilities and rolls found here

    Works thanks!
    I was hoping for something simpler, but this seems to be the only way.

    Using WP_User class is easier. First, grab the user info:
    $ID=”2″;
    $user = new WP_User($ID);

    Now you can grab the wp_capabilities array and do what you want with it.

    E.g. test for a role capability:

    if ($user->wp_capabilities[‘administrator’]==1) {
    }

    E.g. print a list of user’s roles as an array:

    print_r(array_keys($user->wp_capabilities,”1″));

    Details on WP_user:
    http://core.trac.wordpress.org/browser/trunk/wp-includes/capabilities.php

    Yet another method:

    global $current_user;

    get_currentuserinfo();

    if($current_user->user_level == 0){ /* Is user subscriber? */
    }

    *See: User Level to Role Conversion to set appropriate value for user_level.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting a WP user’s role’ is closed to new replies.