Support » Themes and Templates » User Role Template Tag

  • Resolved marzar00

    (@marzar00)


    G’day all,

    I have done a search and looked in the Codex and can’t seem to find any template tag for displaying the users level (role).

    Does anyone know if this exists?

    Marzar

Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m bumping this up, because I would like to see this too, so that I can insert bylines on my posts to identify the writer as an administrator, editor, contributor, etc.

    Having a template tag for this is a great idea. I’ll submit a patch to provide one, but since that will take a while to filter down into a release, here’s some code that will display the user role:

    [Edited – see below]

    Keep in mind this, like the_author* template tags, is intended for use in The Loop. Perhaps much to take in if you don’t know what’s going on, but basically it culls the capabilities bit from the $authordata array that already exists within the post loop, and assigns it to $role.

    This SHOULD work for my intended purpose. Thank you for you assistance and your greater skill.

    😉

    Ah perfect, thanks Kafkaesqui. I can’t wait to get the tag in later releases.

    Cheers.

    Hmm, I should update this with changes I made to the submitted *patch.* Looks more complicated, but makes collecting the role far less likely to encounter problems with the way it’s stored in the capabilities record:

    <?php
    global $wpdb, $wp_roles, $authordata;
    foreach($wp_roles->role_names as $role => $Role) {
    	$caps = $wpdb->prefix . 'capabilities';
    	if (array_key_exists($role, $authordata->$caps)) {
    		echo $Role;
    		break;
    	}
    }
    ?>

    Note that:

    echo $Role;

    prints the role with an initial cap (i.e. Author). Change the line to:

    echo $role;

    for all lowercase.

    Hi kafkaesqui,

    strangely, this only works if I am logged in.. what I want to do though is showing the role of the author on the single.php, no matter if I myself am logged in or not..

    How would I need to change your code to do that?

    Hi ixray,

    That is strange. Try the following modification:

    <?php
    global $wpdb, $wp_roles, $authordata;
    
    if ( !isset($wp_roles) )
    	$wp_roles = new WP_Roles();
    
    foreach($wp_roles->role_names as $role => $Role) {
    	$caps = $wpdb->prefix . 'capabilities';
    	if (array_key_exists($role, $authordata->$caps)) {
    		echo $Role;
    		break;
    	}
    }
    ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘User Role Template Tag’ is closed to new replies.