Forums

How to get role of current user? (12 posts)

  1. jonathaeunice
    Member
    Posted 1 year ago #

    How does one determine the role of the current user?

    I am successfully using current_user_can(), but I need ot know the name of the current role, not just its constituent capabilities. None of Google, the Codex, the forum posts, and even browsing some source have cracked this nut so far.

  2. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Stealing some code from a recent reply of mine, it can be adapted so:

    <?php
    global $current_user, $wpdb;
    $role = $wpdb->prefix . 'capabilities';
    $current_user->role = array_keys($current_user->$role);
    $role = $current_user->role[0];
    ?>

    Now $role will hold the... role of the current user.

  3. jonathaeunice
    Member
    Posted 1 year ago #

    Ah, that does help!

    But it doesn't seem quite right. If the admin has assigned "additional capabilities" to a role, they appear to be listed early in the the array_keys. So maybe something like:

    <?php
          global $current_user, $wpdb;
          $role = $wpdb->prefix . 'capabilities';
          $current_user->role = array_keys($current_user->$role);
          $ncaps = count($current_user->role);
          $role = $current_user->role[$ncaps - 1];
    ?>

    This assumes that the added capabilities would be added before the role. Not sure if that's a safe/correct assumption.

  4. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Oof.

    Your discovery caused me to look at a bit closer at the $current_user object, and I see there a tiny bit smarter way to pull the role from it:

    <?php
    global $current_user;
    $current_user->caps = array_keys($current_user->caps);
    $ncaps = count($current_user->caps);
    $role = $current_user->caps[$ncaps - 1];
    ?>

    But I also don't like having to assume where the role appears in the array. I'm going to dig a bit and see about a more certain way of handling it.

  5. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Well, after looking through the source and some old code of mine, I've come up with this (sort of) compromise solution which benefits from not caring much for where in the capabilities array the role falls:

    <?php
    global $current_user, $wp_roles;
    foreach($wp_roles->role_names as $role => $Role) {
    	if (array_key_exists($role, $current_user->caps))
    		break;
    }
    ?>

    Potential beneficial result is you now get the vars $role and $Role, the latter providing the role name with an initial cap (i.e. Author).

  6. MichaelH
    moderator
    Posted 1 year ago #

    Just a heads-up, but with Trac tikcet 5255 you are likely see some changes in this area so be on the look-out for changes in 2.4.

  7. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Good point Micheal, but considering how long we've seen issues of how roles and capabilities are handled in the db get mulled over, I'm not sure when we'll see a change for the better.

  8. grosbouff
    Member
    Posted 1 year ago #

    got a
    Warning: Invalid argument supplied for foreach() in

    at

    global $current_user, $wp_roles;
    foreach($wp_roles->role_names as $role => $Role) {

    with this last code..??

  9. Kafkaesqui
    Moderator
    Posted 1 year ago #

    grosbouff, try:

    <?php
    global $current_user, $wp_roles;
    if( $current_user->id )  {
    	foreach($wp_roles->role_names as $role => $Role) {
    		if (array_key_exists($role, $current_user->caps))
    			break;
    	}
    }
    ?>
  10. gabrielz
    Member
    Posted 1 year ago #

    Kafkaesqui,

    It doesn't seem to work for me. For all users but Administrator, the variable is empty.
    I am trying to get the current user role, because my system should only show some categories for specific roles.
    Thanks (and sorry my poor English).

  11. Kafkaesqui
    Moderator
    Posted 1 year ago #

    gabrielz, if you do this:

    <?php
    global $current_user;
    print_r($current_user->caps);
    ?>

    what displays for your non-admins? Also, please note the WordPress version you are using.

  12. zoinks
    Member
    Posted 1 year ago #

    Awesome code! I incorporated it into my /wp-admin/edit-form-advanced.php to achieve the "TODO: ROLE SYSTEM" ie....

    if($role=='administrator' || $role=='editor')
    	{ $HideStuff = ' style="display:none;"'; }
    else
    	{ $HideStuff = ''; }

    Then I drop $HideStuff into where I don't want non-admins and non-editors to have access to.

Topic Closed

This topic has been closed to new replies.

About this Topic