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 am using custom user roles which I create and assign by using a plugin. I was wondering how I could retrieve the user's role and store it in a PHP variable.

What I am trying to achieve is something like this:

Let's say I created 3 custom user roles: role_apple, role_banana and role_carrot

if ( 'role_apple' === $user_role ) {
    echo 'User is an apple';
}
elseif ( 'role_banana' === $user_role ) {
    echo 'User is a bannana';
}

How this can be achieved?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You don't need to store anything, just check the current logged in user via user_can:

global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "role_apple" ) ){
  // do something
}
share|improve this answer
    
Hey, I tried the code you gave me but it won't function...I tried it with two accounts, one with the role "Administrator" and one with the role "User" but it doesn't work for some reason. I tried it like this global $current_user; get_currentuserinfo(); if ( user_can( $current_user, "Administrator" ) ){ echo "Admin"; }else{ echo "other"; } –  Kaneda Jan 19 '13 at 14:29
    
it's case-sensitive, try administrator all lower-case. –  Milo Jan 19 '13 at 16:32
    
Hey thanks a lot :) Works perfect. Didn't know it was case sensetive. –  Kaneda Jan 20 '13 at 11:06

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.