1483 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

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'; }
28 posts
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+4 more
onokazu says
$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.

1483 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

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.

766 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Sells items exclusively on Envato Market
  • Made it to the Authors' Hall of Fame
+6 more
LCweb says

Take a look to this ;)

1483 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

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.

766 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Sells items exclusively on Envato Market
  • Made it to the Authors' Hall of Fame
+6 more
LCweb says

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

by
by
by
by
by
by