How to get the currently logged in user's role in wordpress?


Question

How to get the currently logged in user's role in wordpress?

1
13
9/22/2009 6:04:32 AM

Accepted Answer

Assuming you have the user id ($user_id) something like this should work:

$user = new WP_User( $user_id );

if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )
        echo $role;
}

Get the user id from your session.

16
9/22/2009 6:12:30 AM

If you don't know the user id, this function will help you (put it in your theme functions.php file)

function get_user_role() {
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    return $user_role;
}

And then, in your template you can get user role by calling get_user_role().

Found it here.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon