Add Gravatars to your Site

Since WordPress 2.5 we have been able to add gravatars into our comments without the need for a plugin. Before this you’d need to use a plugin (or a bit of complex coding) to do the same job.

If you don’t know much about gravatars or why you would want them then read Kevin’s post on Why you should add Gravatars to your Blog. He briefly mentioned the new WP2.5 Gravatar tag, however I’ll go into its usage further.

The Gravatar Tag

The new tag to do this job is get_avatar();. It accepts a few parameters:

Author Email/ID
The ID or email of the author of the comment. Can also accept the full $comment data structure.
Size
The size to display the gravatar at. The default is 96px. (Optional)
Default
The default icon/image to show if no gravatar is found for the user. Usually the whole image path. (Optional)

An example of usage of this tag is:

<?php echo get_avatar($comment->comment_author_email, $size = '40', $default = 'http://www.domain.com/images/blank-gravatar.gif' ); ?>

This would then display a gravatar for the comment author’s email at a size of 40*40px. If no gravatar is found for the email address then it would display the blank-gravatar.gif image instead. The output of this would look similar to:

<img alt='' src='http://www.gravatar.com/avatar/pathcode' class='avatar avatar-40' height='40' width='40' />

There’s a variety of ways to display the gravatar on your comments, which I can’t really cover here, however a simple solution is to have it float to the right of the comment text, similar to how we have it here on Blogging Tips. Using the Classic theme as an example you’d need to edit your comments.php file, find the lines

<?php foreach ($comments as $comment) : ?>
<li id="comment-<?php comment_ID() ?>">
<?php comment_text() ?>

Insert the new gravatar code between the second and third line e.g.

<?php foreach ($comments as $comment) : ?>
<li id="comment-<?php comment_ID() ?>">
<?php echo get_avatar($comment->comment_author_email, $size = '40', $default = 'http://www.domain.com/images/blank-gravatar.gif' ); ?>
<?php comment_text() ?>

Then in your CSS you can add in the follow to float the gravatar to the right by targeting the class ‘avatar’ which is included in the output.

img.avatar {
float: right;
width: 40px;
margin: 0 0 5px 5px
}

Change the width to the width you’ve set for your gravatars.

Gravatar Settings

You can change the settings for your Gravatars in the WordPress Admin under Settings->Discussion. You’ve got the option to show or not show the Gravatars (they’re set to show by default, but of course you also need the above code to make it all work), and you can also choose the rating allowed as some people’s gravatars you may not deem suitable to have displayed on your site (such as those of an adult nature). ‘G’ rating is the safest to choose and ‘X’ rating allows anything and everything, so choose wisely!

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on June 22nd, 2008 and filed under Blogging
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx

7 Responses to “Add Gravatars to your Site”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. banji says:

    If only I just wait another day, this tutorial will save me a lot of time researching :) . I just activated gravatar yesterday.

    I do have one question though, what code should I put in order to make the gravatar link to the author’s website (if any). If you have a look at my current setting, it is just an image without any link.

    Thank you for your time

  2. Sarah says:

    Hi Banji, sorry for being a day late! Anyway, the code to link the gravatar (nice idea by the way!) would be to wrap the function within a link and use the comment_author_url() function. ie.

    <a href="<?php comment_author_url(); ?>"><?php echo get_avatar($comment->comment_author_email, $size = '40', $default = 'http://www.domain.com/images/blank-gravatar.gif' ); ?></a>

    Hope that does the trick for you :)

  3. banji says:

    Thanks Sarah, it work! But another problem which is not related to the gravatar altogether arises. There’s a blue border to the image. Usually what I did is to put (style=”border:0;”) inside img code. However in this gravatar php code, I’m not sure how to do that.

    An example of what I want to achieve is just like the ones in this post :) Appreciate the help

  4. Sarah says:

    Hi Banji, the blue border is because it’s linked. What you can do is add the following to your CSS file on the .avatar class selector

    border: 0;

    And that will then remove the border.

  5. banji says:

    Successful! Thanks Sarah, I owe you one :)

  6. Great post Sarah. Since 2.5 came out I’ve seen more and more blogs adding gravatar support so I’m sure a lot of people will find this useful.

  7. Sarah says:

    No problem Banji, glad to know it’s all working now :)

    Cheers Kevin. I must admit, until I wrote this I hadn’t converted over to using the inbuilt tag as the plugin was running fine but I’d rather not use plugins if I don’t need to so it gave me a chance to test it out (and then write about it!).

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Find Out More About Our Newsletter
 

Blogging Tips Sponsors

Blogging Tips Newsletter

 

Blogging Tips Sponsors

 

Latest from the Blogosphere