• 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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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.

    Thread Starter jonathaeunice

    (@jonathaeunice)

    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.

    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.

    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).

    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.

    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.

    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..??

    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;
    	}
    }
    ?>

    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).

    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.

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to get role of current user?’ is closed to new replies.