Using Gravatars in WordPress themes

Gravatars, or Globally Recognised Avatars, have been around for a while now and their popularity has been given a further boost for WordPress users with the arrival of built-in Gravatar support in WordPress 2.5. The most common place where people are likely to want to show Gravatars is in the Comments. However, themes that pre-date WP 2.5 probably lack the necessary code to display them. Luckily, WordPress gives us a very easy way to add gravatars to theme template files. This article show you how to add Gravatars to your Comments.

Get yourself a Gravatar!

If you haven’t done so already, go to and sign up for a Gravatar account. It’s free and quick to do, and once you’ve uploaded the image that you will use for your Gravatar, you’re good to go.

Note that your Gravatar is associated with your sign-up email address. If, like me, you are someone who uses several email addresses for registering and /or commenting on different sites, the good news is that you can add other email addresses to your Gravatar account and assign images, or not, as you wish.

To display your Admin’s Gravatar make sure that the email address associated with the Admin User Profile in WordPress is registered in your Gravatar account.

The get_avatar() function

To access the Gravatars in WordPress we simply need to use this function in the appropriate place in our theme files:

get_avatar( $id_or_email, $size = '96', $default = '' );

As you can see, the function takes three parameters:

$id_or_email This identifies whose Gravatar we should display. We are going to use it in our Comments, therefore we can simply use $comment for this parameter and WordPress will automatically get the Gravatars for each comment.

$size This sets the Gravatar image size in pixels. The size is up to you, so choose a size which will fit in with your theme layout. If you don’t specify a size here, the default is 96 x 96 pixels.

$default This allows us to specify a path to a default Gravatar image in the event the commentor doesn’t have a Gravatar. Generally, we can forget about this parameter because we can define a default gravatar in our WordPress Admin (see below). However, if you want to define your own default Gravatar, you can use this parameter to indicate the URL to your default image.

Adding Gravatars to Comments

Now that we have a basic understanding of what a Gravatar is, and know the WordPress function we are going to use, it’s easy to add Gravatar support to an existing theme’s Comments. Simply follow these steps:

Enable Gravatars in our WordPress Admin

Go to WP Admin>Settings>Discussion and find the Avatars section.

Avatars Settings in WordPress Admin
Click image for larger version.

Select Show Avatars, then set up the other options as per your requirements.

Adding the get_avatar() function to comments.php

Open up your theme’s comments.php file and look for the block of code that prints out the comments. Depending on the theme, generally this will commence with either:


      A typical example of this section of code will look something like this:

      1. on comment_approved == '0') : ?> Your comment is awaiting moderation.

      Assuming we are going to add the Gravatars alongside the comment text, we now need to find the specific code that displays the comment text:

      We can now simply add the get_avatar() function to this code as shown below:

      Note: we have to echo the function so that the Gravatar is output on to the screen. For this example I’ve chosen a Gravatar size of 80px.

      Adding styles to the Gravatars

      To get the layout of the Gravatars looking nice we will probably need to style the Gravatar image, and we’ll certainly need to clear any CSS float included in these styles. Here are a couple of options for styling the Gravatars:

      1. Using the CSS stylesheet: The get_avatar() function automatically adds classes of avatar and avatar-$size to the Gravatar IMG tag.

      Therefore we can simply add a class “avatar” to our stylesheet and style it as we like. In the case of default Gravatars the function automatically adds a class of avatar-default. Once again, we can simply add a class “avatar-default” to our stylesheet and style it accordingly.

      2. Inline styles: Alternatively, we can simply add an inline style like this:

      Note that the new line of code is wrapped in a DIV. To make sure we don’t have any problem with the float:left; style, we can add a float clearing method and the final code will look like this:

      That’s it! A really easy and effective way of getting Gravatars into our Comments, thanks to a handy new WordPress function get_avatar() and a 2 minute tweak of our comments.php file.

      Further ideas:

      Although in this example the Gravatars were placed immediately alongside the comment text, you could opt for an alternative layout – it’s entirely up to you. The only thing you have to remember is to ensure that the get_avatar() function is inserted between the foreach… and endforeach PHP commands within your comments.php file.

      If you want to see another way of using Gravatars in your theme, find out how to Create Author Pages.


      1. Thanks for posting these instructions, I found some other site which seems to be very popular, but the instructions were awful. Yours were very easy to follow.


      2. Thanks for the nice comment JP! Glad you found the article useful.

      3. Thanks for the help! I know WP 2.7 has avatar stuff built-in, but that doesn’t matter when your theme isn’t compatible.

      4. @ Brad

        Exactly! Which reminds me… I should write something about Threaded comments and Sticky Posts! 🙂


      5. Hi, Ade

        You’ve a great blog design completely with nice post. I love your code snippet tutorial with easy step by step for following.
        This guide has been successfully applied to my blog theme.
        Keep going up to write tips, guide and other instruction.

        Ups, I’ve an idea. What if you write a guide how you create a style of your blog comment. It’s so different, I can’t find this style anywhere.


      6. I must say that it took me some time before I realized that changing comments’ layout in WP 2.7 is impossible :o) So I downgraded the comments.php to version 2.6.5 and voila :o) Works perfectly for me.

      7. @ Zimmi,

        Thanks for your comment.

        It’s not completely impossible – but it’s not as obvious as with 2.6. Actually, I’ll add that to the list of posts that are in the pipeline – “Styling Comments in WP2.7”


      8. For some reason when I set my default avatar in the wordpress admin panel it is not registering. I am getting the default blue gravatar even though i have selected the mystery man. Any suggestions?

      9. Anyone found a way to cache gravatars locally without using WP-supercache or something similar? I only want to cache gravatars, nothing else.

      10. This helped me a lot! Thank you!

      11. Thanks. Very good article …

      Leave a Comment

      XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


      five × = 35