1509 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
+2 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'; }
31 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has referred 50+ members
  • Has collected 10+ items 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.

1509 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
+2 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.

878 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 ;)

1509 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
+2 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.

878 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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by