Hi, I am working on a new plugin for CC and was wondering if there is a better system for determining current user role? I was a little surprised that there is no function in WP that would return string with user role (I know roles are deprecated in favor of current_user_can – capablities), so I was wondering if I am doing things the right way, or there is some better way to do this.
if (is_user_logged_in()){
if (!current_user_can('delete_posts')){ $user_role = 'Subscriber'; }
else if (!current_user_can('delete_published_posts')){ $uset_role = 'Contributor'; }
else if (!current_user_can('read_private_pages')){ $user_role = 'Author'; }
else if (!current_user_can('edit_dashboard')){ $user_role = 'Editor'; }
else if (!current_user_can('manage_network')){ $user_role = 'Administrator'; }
else { $user_role = 'Super Admin'; }
} else { $user_role = 'Anonymous'; }
$current_user = new WP_User(wp_get_current_user()->id);
$user_roles = $current_user->roles;
$user_roles will then contain an array of roles the current user belongs to.
Hope this helps.
Works like a charm, I knew there has to be something easier. I’ve already checked out that function yesterday, but I overlooked that it returns role too.
Tnx for help.
- Sold between 100 000 and 250 000 dollars
- Author was Featured
- Item was Featured
- Most Wanted Bounty Winner
- Bought between 10 and 49 items
- Exclusive Author
- Referred between 50 and 99 users
- Has been a member for 2-3 years
LCweb said
Take a look to this![]()
Yeah I knew about that, but note this: Do not pass a role name to current_user_can(), as this is not guaranteed to work correctly.
- Sold between 100 000 and 250 000 dollars
- Author was Featured
- Item was Featured
- Most Wanted Bounty Winner
- Bought between 10 and 49 items
- Exclusive Author
- Referred between 50 and 99 users
- Has been a member for 2-3 years
OriginalEXE said
Yeah I knew about that, but note this: Do not pass a role name to current_user_can(), as this is not guaranteed to work correctly.
Yes, you have to use capabilities

