WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have functions that have to execute based on the user roles. How do I check the user role so that I can insert the respective function under their role?


if ($_GET['role'] == "free member" ) {//insert funtion 1}

elseif  if ($_GET['role'] == "sliver member" ) {//insert funtion 2}

else {//insert funtion 3}

I tried using current_user_can, like this :

global $get_currentuserinfo, $current_user; 
if( current_user_can('free_member') ) {//insert funtion 1} 
elseif current_user_can('sliver_member'){//insert funtion 2} 
else {//insert funtion 3} 

This work with default role like "editor","author", "contributor". but seem doesn't work with custom role that created by role managing plugin. I am using Advanced Access Manager to create the custom role by the way.

Since this role checking snippet is using in writing panel, so what approach is best to check the user role?

share|improve this question
Your approach is the best, but who knows how the plugins were written. Try use a default function like add_role. – Wyck Apr 30 '12 at 15:49
up vote 0 down vote accepted

Looking around Google for a few minutes yielded several promising results.

Here's a more detailed one adapted from a snippet on The Code Collective:

function get_user_roles( $user_id ) {
    $user_roles = [];

    $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 ) )
                $user_roles[] = $role;

    return $user_roles;

This will return an array of the user passed in as $user_id.

share|improve this answer
thanks...your info is very useful to me... – dev-jim Apr 30 '12 at 16:24

Your Answer


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.