Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to classify the user according to their role, so I need to find the user role by using the user_id alone (not only for logged in users but for all the BP users).

Something like this $user_role=role($user_id);

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Try this function:

function get_user_role($user_id){
    global $wpdb;
    $user = get_userdata( $user_id );
    $capabilities = $user->{$wpdb->prefix . 'capabilities'};
    if ( !isset( $wp_roles ) ){
        $wp_roles = new WP_Roles();
    }
    foreach ( $wp_roles->role_names as $role => $name ) {
        if ( array_key_exists( $role, $capabilities ) ) {
            return $role;
        }
    }
    return false;
}

I've not included any exception handling like whether the user exist or not, so you can do it yourself or in case you are getting the user ids list wont even be necessary.

share|improve this answer
    
Thank you much :) –  user1460692 Sep 24 '12 at 14:10
    
Always Happy to Help :) –  Gagan Sep 25 '12 at 18:41

I have created a function to get user role from userid.

function get_user_role( $user_id ){

  $user_data = get_userdata( $user_id );

  if(!empty( $user_data->roles ))
      return $user_data->roles[0];

  return false; 

}

call get_user_role() function to get the role of the user and pass the userid as parameter. It will return user role else will return false .

share|improve this answer
    
Thankyou much :) working perfectly :) –  user1460692 Sep 24 '12 at 14:09
    
Bam! Works AWESOME. Thanks. –  Jason Green Jun 29 '13 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.