Nerd tech, entertainment, and web development blog

WordPress Hacks: Add Gravatars to Your Comments

You’ve probably seen it by now, the little icons beside posts. Well, they are gravatars and if you don’t have one, you really should. Not only is it great for branding, but also a great way to make people stop and read what you have to say.

Of course, for websites branding isn’t the purpose; it is making your comments section look nerdy-awesome (which is better than awesome, btw). Yes, impNERD uses them, they are just quite small. On average, websites use around 64×64 or 32×32–both easy to see and easy for commenters to brand.

Gravatar

Gravatars have been implemented in WordPress since the 2.5 release. If you aren’t upgraded to the latest version, you can use one of the many known plugins. However, this article is for WordPress version 2.5 and above.

So lets get started, shall we?

The main function we’ll be working with is get_avatar();

Without any additional features, get_avatar(get_the_author_id()) can be added directly into the comment loop. But who wants that? We want these to look awesome!

What you can edit:

  • User ID (required) – The id or email of the gravatar you want to show. Within comments you can use either $comment or get_the_author_id() depending on your theme.
  • Size (optional) – The default is 96×96. Why did they choose that size? No clue.
  • Default (optional) – The default image if the commenter doesn’t have a gravatar. This can also be changed in the wp-admin/settings/discussion (with a limited selection). You can create your own image and use it as well by using the default variable when calling the function.
  • Rating (optional) – The maximum allowed rating a gravatar can have to be shown on your website. The default is rated G. This can be changed in wp-admin/settings/discussion.

Know what you want to change? Alright, good.

Find your loop and where you want the gravatars to be shown. Now lets add the gravatars:


<php echo get_avatar($comment, $size=32, $default='http://example.com/test.php'); ?>

Nice. Want to add some styling? Put a <span> around the image with a class to make it look really nice.

Written: Oct 15, 2008


Related posts »

6 Responses to "WordPress Hacks: Add Gravatars to Your Comments"

  • Scott Lackey
    October 15, 2008 @ 1:17 pm


    Not sure why you say that gravatars for websites aren’t branding elements. Every single thing you do on your site or blog contributes to building your brand…even the smallest thing like a gravatar.

  • Gary R. Hess
    October 15, 2008 @ 2:00 pm


    @Scott you are definitely right. It was just bad wording on my part.

  • Dennis Edell
    October 16, 2008 @ 11:21 am


    2.3.3 here LOL. I’m waiting for 2.7, all the disaster stories kept me away from 2.5 and the subsequent fixes.

  • Margaret
    October 17, 2008 @ 10:22 am


    Ok, call me confused, but I thought WP 2.6 includes gravatars as a standard and all you have to do is go into your settings to configure. It even gives you a couple of different options from using Blogcatalog (I think that’s right) avatars to using a couple of generic sets that must come with WP. I believe I have the monsters enabled on my blog.

    So, why is your function different from what is included in WP 2.6 and why would I want to use it instead of the built in function?

    Thanks!

    ê¿ê

  • Gary R. Hess
    October 17, 2008 @ 12:27 pm


    Margaret, it technically is the built-in WP function. The only difference is that your theme probably came with the function already in, most themes don’t (especially the older).

  • Holly
    October 19, 2008 @ 10:01 pm


    This is a really great tutorial; thanks!