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?

| improve this question | | | | |

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

global $current_user;
if ( user_can( $current_user, "role_apple" ) ){
  // do something
| 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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