Wordpress, How to insert the Gravatar icon into the comment section

Posted by Daniel - 1,472 Views

GravatarWhen I was blog-walking to several blogs, I saw not all of them but most of them use Gravatar icons to represent the commentator’s email addresses - its cute. Gravatar or Globally Recognized Avatars is basically a 80 x 80 pixel image used to represent your email address. If you do not have any gravatar yet you can got to gravatar website then signup yourself to get it - it’s simple.

OK, now lets find out how you can retrieve the gravatar of each commentator’s email address on your blog. First open your comments.php file - commonly it is placed on your current theme folder - with your text editor.

Find this line of code within the file:

<?php comment_text() ?>

When you already found it, next to do is to replace that line with the following block of code but I think it’s a good idea to backup the original content of your comments.php file since not every theme has as simple code as my current theme.

<?php
if (get_comment_type() == ‘comment’) {
echo "<img src=’http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email)."’ style=’padding-top: 15px’ />";
}
else {
echo "<img src=’http://averagecoder.net/wp-content/uploads/trackback.jpg’ style=’padding-top: 10px’ />";
}
comment_text();
?>

On the above code you can see the get_comment_type() function. It’s a WordPress hook function to get the comment types which only has 3 return values, and they are:

  1. comment
  2. trackback
  3. pingback

I only need to get the Gravatar for normal comment and mark the other comment types (usually a trackback) with the other image so that is why you see the IF statement there to get the intended result.

Cool, basically it’s done and I hope the implementation on your code will be as easy as copy-pasting it. Thanks to ridhocyber for helping me with this one.

Enjoy…

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008

2 Responses to “Wordpress, How to insert the Gravatar icon into the comment section”

  1. Daniel Says:

    This is a test to check the Gravatar Icon…

    Cheers :)

  2. Mahesh Says:

    Thanx for the gr8 info.. Trying it on my blog now!

    Mahesh’s last blog post: Windows 7 Keyboard Shortcut keys



Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.