WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] User Role Template Tag (8 posts)

  1. marzar00
    Member
    Posted 6 years ago #

    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

  2. billdennis5
    Member
    Posted 6 years ago #

    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.

  3. Kafkaesqui

    Posted 6 years ago #

    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.

  4. billdennis5
    Member
    Posted 6 years ago #

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

    ;-)

  5. marzar00
    Member
    Posted 6 years ago #

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

    Cheers.

  6. Kafkaesqui

    Posted 6 years ago #

    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.

  7. ixray
    Member
    Posted 6 years ago #

    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?

  8. Kafkaesqui

    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic