Adding Gravatars to Posts

With the launch of the Blueprint site, and all the hubbub over the release of Prologue, there are now a couple of good examples of using gravatars for more than just comments. The prologue function is a little weighty to be copied to other themes, since it's built for wordpress.com, so i thought I'd share my simple method. It's almost identical to the method matt posted,


<?php $md5 = md5( strtolower(get_the_author_email()) );
$default = urlencode( get_bloginfo(template_directory) .'/icons/author.png' );
echo "<img alt='avatar' class='photo' title='photo' src='http://www.gravatar.com/avatar.php?gravatar_id=$md5&size=50&rating=X&default=$default'  />";
?>

Note that this assumes your theme has an author.png inside an icon folder. Change that line to reflect the location of your default icon, or use a blank default. If you're using Sandbox, or any other hCard-enabled wordpress theme, you can insert this inside <div class="vcard">, for added microformat-y goodness. Otherwise, you can put it anywhere you like. I've also added the same code to the top of my author.php file, inside <div class="archive-meta">.

5 Comments

  1. Posted 6 Feb 2008 at 9:01 |
    Permalink Quote

    Don’t forget to lowercase the email address first.

  2. Posted 7 Feb 2008 at 8:17 |
    Permalink Quote

    Thanks for the heads up, fixed :)

  3. Posted 12 Feb 2008 at 6:49 |
    Permalink Quote

    This is pretty slick. I am going to be using that one for sure. Just need to add a conditional to either use the avatar plugin I am using and if that doesn’t exist, use the gravatar. That should be pretty simple though.

  4. Posted 14 Feb 2008 at 12:58 |
    Permalink Quote

    your blog is the other example that came to mind of author avatars, but I wasn’t sure how you were doing it, so I skipped mentioning you, in case I was wrong about where your avatars came from. which I was.

  5. Posted 26 Feb 2008 at 10:08 |
    Permalink Quote

    The author and recent comments on my main blog are both from gravatar using http://bloggs.be/wpmu/gravatar/ and I also have both my own avatar system and gravatar in the blog comments. Some think having both avatars in my actual comments is a little much, but it is something that I am sticking with for now.

Post a Comment

Your email is never published nor shared. Comments that do not follow the guidelines may be removed. By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Required fields are marked *
↓ Preview