Displaying Author Meta Information in Wordpress 2.8


Display unique author information

One of the (many) nice updates the comes bundled in Wordpress 2.8 is with the_author_meta() template tag. This tag allows a developer to pull and display specific parts of any user’s information within a theme. Even though variations of this have been included in previous releases, this version has simplified it to a much easier syntax.

In this quick overview, we’ll look at some potential uses for the recently overhauled Author Meta template tag. Keep in mind that all of these demonstrations are done within a Wordpress 2.8 installation. If you do not have this version running, these tags will not work as intended.

Displaying the Data

Especially if your site has registered users, this is an easy way to personalize your blog’s theme for each one. Let’s say you wanted to provide users a way to contact you by email:

<p>Get in touch with <a href="mailto:<?php the_author_meta('user_email'); ?>"><?php the_author_meta('user_firstname'); ?> via Email</a></p>

The above code will link to the email address of the author using their first name as anchor text.

Sample Output: Get in touch with Zach via Email

With the new author_meta options, information is displayed when the tag is called. If you’re looking to format the data first, you’ll need a different tag. Lucky for you, that’s exactly what the next section covers.

Manipulating the Data

Keep in mind that the_author_meta() tag is only meant to be used to display the contents. If you’re looking to manipulate or format the contents of a field, you’ll have to use a slightly different variation of the tag. This is where get_the_author_meta() comes in.

This template tag follows the same idea of retrieving author meta information, but it does not automatically display the contents. Here’s an example using date formatting:

<?php
   //Format the author's registration date
   $register_date = date("m/d/Y", strtotime(get_the_author_meta('user_registered')));
?>

And this code will give us a formatted output from the results:

<p><?php the_author_meta('user_firstname'); ?> has been with us since <?php echo $register_date; ?></p>

Sample Output: Zach has been with us since 06/01/09

As you can see, the result finds the registration date of the user, and then formats it from the original timestamp into something much more pleasant to read. Not bad for a couple lines of code!


Build an Updated Author Box

One of our earliest tutorials was on how to create an “About the Author” box within Wordpress 2.7. The tags used there are now deprecated with this update, so let’s take a moment to refresh our code and stay fresh.

Fresh new author box

Originally, the code to render our author box with a gravatar, linked author name, and description looked something like this:

<div id="authorbox">
   <?php if (function_exists('get_avatar')) { echo get_avatar(get_the_author_email(), '80' ); }?>
   <div>
      <h4>About <?php the_author_posts_link(); ?></h4>
      <p><?php the_author_description(); ?></p>
   </div>
</div>

As you can see, the previously used tags don’t really follow a single format. Now let’s take a look at the same result using the updated Wordpress 2.8 tags. For the sake of education, we’ll be replacing the author post link with an anchor to their website’s URL instead.

<div id="authorbox">
   <?php if (function_exists('get_avatar')) { echo get_avatar(get_the_author_meta('user_email'), '80'); }?>
   <div>
      <h4>About <a href="<?php the_author_meta('user_url'); ?>"><?php the_author_meta('display_name'); ?></a></h4>
      <p><?php the_author_meta('description'); ?></p>
   </div>
</div>

Even though this didn’t condense the code, it does become easier to skim through on the code end. For the most part, it’s very easy to read the tags involved.

Notice that in the case of our gravatar email, we’re only interested in getting the value as a string and not in displaying it. This is the something that is easy to get caught up with, and should be one of the first checks whenever you’re experiencing errors.

Break It Down!

As demonstrated above, this seemingly tiny update makes it monumentally easier to pull specific information about the author and display it as you please. From here, it’s just a matter of finding new ways to break down specific components in new and creative ways.

Have any ideas for clever applications? Let the rest of us recently upgraded Wordpress fiends in on it too!

  • Stumble It!
  • Bookmark It!
  • Tweet it!

About Zach Dunn

Zach is a partner and interface designer at One Mighty Roar from Massachusetts, USA. Follow him on Twitter @zachdunn.

 

Discussion

  1. Montana Flynn

    June 12th, 2009 at 2:10 AM

    Real nice, I have mine hardcoded into my theme but this would be useful for guest posts and blogs with more than one author!
    [rq=4694,0,blog][/rq]Save some tree with virtual business cards!

  2. Callum Chapman

    June 12th, 2009 at 3:06 AM

    I’m hoping to get some guest bloggers on my blog soon, so this should turn out to be pretty useful! Thanks :)
    [rq=4807,0,blog][/rq]10 Beautiful Black & White Illustrations

  3. sonichtml

    June 12th, 2009 at 5:04 AM

    Nice~ thank you for shared..

  4. Sumesh

    June 12th, 2009 at 6:45 AM

    That’s a useful function – I’ve been looking at other slightly complicated workarounds to show author information, as it is on TUTS sites, Makeuseof.com etc.

    Saved on Delicious :)
    [rq=5323,0,blog][/rq]OldVersion.com: Get older versions of software

  5. Hezi

    June 19th, 2009 at 10:55 AM

    that simply ROCKS!

  6. lossendae

    June 22nd, 2009 at 7:34 PM

    That’s cool and easy!

  7. henry

    June 24th, 2009 at 7:58 PM

    once again i have to thank you for making it easy to find helpful and useful wp tips!!!

  8. Tyler - Building Camelot

    July 4th, 2009 at 11:30 PM

    And what about the CSS? Can you use the same CSS code from the previous article and apply it here?
    .-= Tyler – Building Camelot´s last blog ..Weekly Round Table: Happy 4th of July! =-.

  9. Zach Dunn

    July 5th, 2009 at 4:49 PM

    @Tyler

    The CSS should be able to translate over, since we’ve kept the same basic HTML structure.

  10. Tyler - Building Camelot

    July 6th, 2009 at 3:17 PM

    @Zach: Thanks for the response and for the code! It works perfectly and I’ll be sure to sent people this way if they ask how I got the code to work.

    One last thing if you don’t mind…what would I need to add to make sure this shows up on single pages only. I know it’s via “is_single()” but I don’t know how to modify the code to make the changes.

    Thank you!
    .-= Tyler – Building Camelot´s last blog ..Following Up With Vincent From The Dad Jam =-.

  11. Zach Dunn

    July 6th, 2009 at 3:21 PM

    @Tyler

    If you only need it to show up on blog posts/single pages, the answers actually pretty simple. You’ll want to put this code into the single.php template, right below the post loop.

  12. Andrew

    July 20th, 2009 at 4:27 PM

    Thank you for the easy to follow tutorial!

    How would I go about setting it so a post by the “Admin” account doesn’t show the author box?

    Also, Primer has a lot of guest posts, how would you recommend achieving an author box without making new accounts for every person that does a one-off guest post?

    Thanks again!
    .-= Andrew´s last blog ..Are You That Guy? =-.

  13. Jason Saggers

    September 16th, 2009 at 7:03 AM

    This works perfectly. the previous version didn’t for some reason.
    Could i just put this on the pages just as easy?

  14. mazyoyo

    October 5th, 2009 at 6:12 AM

    great tips, thanks for share.

  15. hanniz

    December 1st, 2009 at 11:31 AM

    Hi,

    how do you code this, if you want to display the author information before the list of the author (= The Loop). Do you need two Loops?

  16. Michael Krapf

    February 16th, 2010 at 9:43 AM

    This is very helpful. I’m stumped on how to get this to work for a “user” instead of “author”.

    has been with us since

    I’ve tried everything. Any ideas?

    Thank you

Join the Conversation!

Remember: Life's not all doom and gloom, so please keep it constructive. If we've made an error or missed something big, please let us know! Learning is revisions, after all.

CommentLuv is Enabled

 

Sponsors

Advertise on Build Internet!