Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Displaying Author Meta Information in WordPress 2.8

Displaying Author Meta Information in WordPress 2.8


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!

Wordpress.com stats not installed! Posted Thursday, June 11th, 2009 / Back to Top

I this post. Tweet
SPONSOR

29 Comments 53 Mentions

  1. Montana Flynn Author Editor

    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!

    June 12, 2009 · Reply

  2. Callum Chapman Author Editor

    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

    June 12, 2009 · Reply

  3. sonichtml Author Editor

    Nice~ thank you for shared..

    June 12, 2009 · Reply

  4. Sumesh Author Editor

    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

    June 12, 2009 · Reply

  5. Hezi Author Editor

    that simply ROCKS!

    June 19, 2009 · Reply

  6. lossendae Author Editor

    That’s cool and easy!

    June 22, 2009 · Reply

  7. henry Author Editor

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

    June 24, 2009 · Reply

  8. Tyler – Building Camelot Author Editor

    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! =-.

    July 4, 2009 · Reply

  9. Zach Dunn Author Editor

    @Tyler

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

    July 5, 2009 · Reply

  10. Tyler – Building Camelot Author Editor

    @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 =-.

    July 6, 2009 · Reply

    • Zach Dunn Author Editor

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

      July 6, 2009 · Reply

  11. Andrew Author Editor

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

    July 20, 2009 · Reply

  12. Jason Saggers Author Editor

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

    September 16, 2009 · Reply

  13. mazyoyo Author Editor

    great tips, thanks for share.

    October 5, 2009 · Reply

  14. hanniz Author Editor

    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?

    December 1, 2009 · Reply

  15. Michael Krapf Author Editor

    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

    February 16, 2010 · Reply

  16. Web Design Author Editor

    this was very helpful thanks for the info

    April 22, 2010 · Reply

  17. Billy McGovern Author Editor

    I have added the php to my single.php file and I reference the “authorbox” in my css file but, I still cant get it to format. It shows up but just as text and an image, no organization. Help?

    June 13, 2010 · Reply

  18. Dwaynne Author Editor

    Hi, Zach. I need some guidance here. I defined some custom fields in functions.php for my user profile screen and now I want to use a value entered in one of those fields to filter a list.

    Specifically I have a custom field named ‘Branch’ and I want to say if the value of Branch is St. Augustine then show me all the staff members in St. Augustine branch. I know the key to it is the get_the_author_meta/the_author_meta but I am not sure how to structure the query.

    Thanks in advance.

    August 17, 2010 · Reply

  19. best roulette system Author Editor

    Good entry thanks, needed some more pictures though.

    October 10, 2010 · Reply

  20. Free stickers Author Editor

    Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is fantastic blog site. A fantastic read. I will definitely be back…

    May 22, 2011 · Reply

  21. mmoVZ Author Editor

    Thanks for this tutorial. I extended it a bit by adding the total number of posts and comments made by that author.

    August 3, 2011 · Reply

  22. Jacquelyn Degrate Author Editor

    continue with the the nice work on the site. Do like it! :p Could use some more frequent updates, but i am sure you got some better things to do like we all do.

    August 22, 2011 · Reply

  23. Virgil Author Editor

    Learn a lot, really useful information.

    February 21, 2012 · Reply

  24. Paddy Author Editor

    Your post information is very detailed.

    February 28, 2012 · Reply

  25. reverse phone lookup Author Editor

    I certainly did not realize that. Learnt something new right now! Thanks for that.

    March 23, 2012 · Reply

  26. LV hilton Author Editor

    connected with handbags utilised LV shoes It is right here that reproduction

    March 28, 2012 · Reply

  27. cloud based filtering Author Editor

    Thanks for sharing and posting meta.This helps us blogger using this meta.

    July 24, 2012 · Reply

  28. Ibrahim Author Editor

    It’s really great man :)

    August 22, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top