WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting a WP user's role (5 posts)

  1. ninnypants
    Member
    Posted 5 years ago #

    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?

  2. ClaytonJames
    Member
    Posted 5 years ago #

    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

  3. ninnypants
    Member
    Posted 5 years ago #

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

  4. jaredstein
    Member
    Posted 5 years ago #

    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

  5. Capn Code
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.