How to Setup Gravatars for your Blog - Plugin Free!
Posted on March 2nd, 2008. 108 Comments so far. You next?
![]()
Recently I’ve added Gravatars to the comments portion of the site. This is extremely easy to as long as you’re not totally useless in terms of copying and pasting. Fear not of messing your blog’s important code up, for this plan is fool proof.
What is a Gravatar?
To start, an avatar is a small image associated with a user, found a lot on forums and are beginning to appear more and more on blogs. Gravatar is a service which allows for “Globally Recognized Avatars“. Basically, you can sign up for the service and your email will be associated with an image you upload. After that, it could’t matter less if you ever visit their site again!
From that point in time, every time you leave a comment at a blog with your valid email address, your image will appear next to your name, providing the owner has added a few simple lines of code.
Their site puts it like this:
Gravatar aims to put a face behind the name. This is the beginning of trust. In the future, Gravatar will be a way to establish trust between producers and consumers on the internet. It will be the next best thing to meeting in person.
That might be reaching a little, but by now you should have a brief understanding.
What are the Benefits?
Draw attention to yourself. Most people comment on a website hoping for a little bit of traffic, so it makes sense that using any means within your arm’s reach is beneficial to you. A minute long process of signing up for a service and you get that attention.
People have been known to include scantily clad women, which certainly warrant a few clicks from weary lurkers in the depths of your archives. In short, a Gravatar can do a few things for you:
- Personalize. Add a picture of yourself and people will see the face behind the name and the words you write.
- Capture attention. As mentioned above, if you want clicks over a response or any shred of respect, go for shock or surprise value.
- Get recognized. Comment a lot? Authors and readers like will recognize the avatar of a commenter who consistently posts insightful musings.
In the end, you spend two minutes or so signing up and uploading a picture, and you will see the results soon after.
How to do it
This is a guide custom tailored for WordPress users, but will work on any PHP driven system. Open up comments.php and follow these steps:
- Setup your variables. To generate the image you need to send some simple information.
<?
$email = $comment->comment_author_email;
$default = “”; // link to your default avatar
$size = 40; // size in pixels squared
$grav_url = “http://www.gravatar.com/avatar.php?gravatar_id=
” . md5($email) . “&default=” . urlencode($default) . “&size=” . $size;
?>There isn’t much in there to understand. Set the
$emailvariable to whatever works with your system. The one in place works with WordPress. After that, upload an image you want to serve when the user doesn’t have a Gravatar, input a size and you’re good to go. - The HTML code. Next, just insert this code where you want the image to appear:
<img src="<?=$grav_url ?>" height="<?=$size ?>" width="<?=$size ?>" alt="User Gravatar" />That will display the user’s image.
That’s it! As you can see it won’t take long. You can do this simply and without a WordPress plugin.
What now?
You can now style the image to match your site. You can view a single post on this site to see what it looks like. I have mine setup to give every user 70*70 image that floats to the left of their comment. I think it looks pretty nice, considering the benefits it allows my readers and time it took.
So if you’re reading this, or leave comments on any Gravatar enabled site, I implore you to sign up and get your own image.








76 comments
March 2nd, 2008 at 11:06 PM
Hey Connor, this is excellent. I’m going to try it out right now.
One thing to have caution against is having the PHP shorthand in your code since not all hosts have it enabled.
March 2nd, 2008 at 11:25 PM
Sorry to bombard your comment’s section
I used this code to implement gravatars in the sidebar (mod of yours) to showcase post authors:
And here’s the image code:
March 2nd, 2008 at 11:31 PM
Haha, bombard away. That’s a great example, and the author Gravatar is something I’m actually including in a design I’m working on now.
And I guess if you’re having server troubles with this you can go the long way around of
< ?php echo $grav_url; ?>, etc… instead of< ?=$grav_url ?>.March 2nd, 2008 at 11:34 PM
One thing I forgot to mention in my comment is that you want to add a conditional tag before it:
start with:
and end with after the Gravatar:
March 3rd, 2008 at 12:53 AM
The problem I run into is user who use mixed case email addresses to leave comments. To fix for this, I use strtolower in the call to the email address:
.md5(strtolower($email));
March 3rd, 2008 at 12:54 AM
thanks for sharing, Connor.
mine is still using easygravatar plugin.
will have a try using this method someday.
March 3rd, 2008 at 1:48 AM
Just seeing what it looks like…..
March 3rd, 2008 at 1:52 AM
I just implemented your code on my site and it works perfectly, thanks!! Just need to get myself a gravatar now.
You can see it in action on this post; http://blog.yourwish.co.nz/2008/02/27/25-tips-to-enjoy-your-wedding-day/
March 3rd, 2008 at 3:03 AM
If this is foolproof, I guess that makes me a fool. I can’t seem to get it to work for the life of me!
March 3rd, 2008 at 3:25 AM
I’m going to try it NOW!
March 3rd, 2008 at 3:53 AM
Will try it on my blog. It seems that we are heading towards globalisation.
March 3rd, 2008 at 4:05 AM
Thanks for sharing, nice idea..
March 3rd, 2008 at 4:28 AM
I was considering adding these for my http://www.HatchThat.com redesign but I wonder how many of my commenters will have a Gravatar - I assume this just shows a default if they don’t have one, I’d love it if it would leave the space totally blank instead.
March 3rd, 2008 at 5:05 AM
nice one i’ll try this one right on
March 3rd, 2008 at 5:11 AM
Great tips …
I will using it on my next theme … Thank you very much
March 3rd, 2008 at 5:34 AM
I am a little skeptical of doing this.
Because 2.5 is going to be out in a week, and they have the option to show gravatars and avatars.
http://trac.wordpress.org/ticket/5775
Otherwise, this would have been a very good hack.
March 3rd, 2008 at 6:24 AM
Thanks, it works well, no problems.
March 3rd, 2008 at 7:05 AM
Wow, definitely didn’t expect to wake up with 19 emails in trackbacks and comments. I’m not complaining though
Darran: That’s a good point, though this will stand for those not wanting to update right away. To be honest I haven’t even updated to 2.3. This way also might allow you a more complete control over the style, I don’t know - I haven’t personally test 2.5 yet.
March 3rd, 2008 at 8:10 AM
I think the picture rating is important, you dont want to see triple-x materials in your blog.
additionally add
<?php
$email = $comment->comment_author_email;
$default = ""; // link to your default avatar
$size = 40; // size in pixels squared
$rating = "G"; // [G | PG | R | X]
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=
" . md5($email) . "&default=" . urlencode($default) . "&size=" . $size."&rating=".$rating;
?>
ref → http://site.gravatar.com/site/implement
March 3rd, 2008 at 8:17 AM
good read mate!
March 3rd, 2008 at 8:27 AM
It’s even better (and recommended) to get md5(strtolower(trim($author_email))) so that joe@joe.com and JOE@JOE.COM will actually get their gravatar.
March 3rd, 2008 at 8:47 AM
Excellent post. I’l make use of this coding instead of the easy gravatars plugin in my new blog venture. And Conor, I really love the way you’ve modified the author comment feature of your blog here. Can you post some coding for that?
Cheers
March 3rd, 2008 at 8:49 AM
I’m not going to hack the core files just to implement this Gravatar stuff
March 3rd, 2008 at 9:07 AM
What happens if I didn’t upload a photo to Gravatar?
March 3rd, 2008 at 9:07 AM
Although with a plugin it’s nice to be able to set up locally hosted gravatars.
March 3rd, 2008 at 9:10 AM
@ Dirk
You’re not hacking a core file. The comments.php is a theme file and doesn’t get overwritten when you upgrade.
@ Connor
Thanks for the simple steps. I’ve been contemplating adding gravatars but didn’t want to add another plugin…
March 3rd, 2008 at 11:57 AM
Sweeeeet.
March 3rd, 2008 at 12:57 PM
Awesome, this looks really interesting, especially (and as others have mentioned) because it can be used to show gravatars in other places than in just the comments.
This might be just the thing i need to spice up a sidebar on a theme I am playing with!
Thanks!
March 3rd, 2008 at 1:19 PM
Had this implemented a while now. I used Matt’s suggestion on implementing it. Just one question: what will happen if I have this code in place in my theme and I upgrade to WordPress 2.5?
Good job on the tutorial, by the way.
March 3rd, 2008 at 1:21 PM
Ok, me and my quick fingers. I guess I skipped reading a lot of comments that answered my question already. Sorry.
March 3rd, 2008 at 2:34 PM
Thanks I will be using it on my future blog!
March 3rd, 2008 at 2:40 PM
And maybe to gravatar the author’s pingback as well.
http://www.walkernews.net/2007/10/30/enable-gravatar-in-wordpress-comment-and-pingback/
Ohya, I found that you comments form is not displayed correctly in my XP-IE7 (you might need to check this out)
March 3rd, 2008 at 5:07 PM
I will give this a try over the weekend!
March 3rd, 2008 at 6:51 PM
I was actually wondering how this was done.
March 3rd, 2008 at 8:44 PM
thanks!
March 3rd, 2008 at 8:58 PM
Excellent, and thanks — gonna give it a shot!
March 4th, 2008 at 4:08 AM
Will see if i can implement this on my current blog template. This is fascinating!
March 4th, 2008 at 5:25 PM
Great tutorial. Thanks!
March 5th, 2008 at 7:31 PM
Hi,
Since installing your code, I have gravatars displaying, but it’s only displaying the default one, even though I have a gravatar registered against that email address and I’ve use the uppercase/lowercase solution.
http://blog.yourwish.co.nz/2008/02/27/25-tips-to-enjoy-your-wedding-day/
Any help would be appreciated.
Dawn
March 5th, 2008 at 9:07 PM
Dawn: If you post the exact PHP code you have I should be able to set you off in the right direction. The only reasons it would automatically grab the default is either the account doesn’t exist or you’re not setting the email variable correctly.
March 5th, 2008 at 10:02 PM
Hi Connor,
I have used the exact same code, copied and pasted from your site, the only change made is to set the url for my default gravatar, so my code looks like this:
comment_author_email;$default = "http://www.yourwish.co.nz/img/default.gif"; // link to your default avatar
$size = 40; // size in pixels squared
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=
" . md5($email) . "&default=" . urlencode($default) . "&size=" . $size;
?>
March 5th, 2008 at 10:08 PM
It looks like the front end of that got cut off, but make sure you have:
$email = $comment->comment_author_email;That’s the gamebreaker. The script works 100% (it runs right here), but if you have it exactly it could potentially be an issue with your host. You need to be able to display dynamic images.
That’s more of a whim however, because the default appears fine.
March 6th, 2008 at 3:59 PM
Connor, thanks for your patience in sorting this out for me, it turns out that it needs to be under “
< ?php foreach($comments as $comment); ?>”I just had it outside of the loop where the email variable works!
thanks
Dawn
March 7th, 2008 at 3:25 AM
thank for the inspiration, I added gravatars to my theme, too
March 7th, 2008 at 5:48 AM
Great tutorial… just found out my free host doesn’t support php
March 10th, 2008 at 9:25 AM
Thanks for this tutorial. Great job! Enjoy
March 12th, 2008 at 6:30 AM
Hi and thanks for the tip Connor. I have been trying to get gravatars working properly on my site, when I came across this. But after reading the comments, maybe I should just wait for the 2.5 release - might be a bit easier!!
March 12th, 2008 at 11:17 AM
Great tutorial - I’m unable to get it to work on my site however. Having said that, I upgraded EasyGravatars on another site I host last night, and as of this morning, no gravatars are being pulled in. So before I annoy you to death, I wanted to post here and see if the problem is with my host, the update, or what. I get the default image on both domains, one using your method and one using EasyGravatars. I’ve used the tools on site.gravatar.com and my gravatar shows up just fine. I’m completely stumped!
March 12th, 2008 at 11:20 AM
Michael: This is usually cause by putting the variables above your comments loop. Make sure you’re declaring everything below
foreach($comments as $comment)or something similar.March 15th, 2008 at 11:33 AM
Connor,
For those of us, like Dawn and I, wouldn’t aren’t PHP experts, would it make sense to change your above post / instructions to let users know that the code needs to go below foreach($comments as $comment), as you mentioned in one of these last comments? That was giving me problems until I read through all of the other user feedback.
Also, maybe adjust the code to have the string to lower, and rating stuff, optional maybe, but included above?
March 20th, 2008 at 4:45 PM
Here from Sue - trying to learn how to improve my blog and to see if my gravatar is working.
Btw: Hello from Norway - great blog - keep up the good work!
March 21st, 2008 at 8:25 PM
@Connor
Thanks for the great article. I was getting rather tired of all the plugins that I was installing to achieve simple results like this.
@ Dawn
Thanks for having the same problem I was.
March 30th, 2008 at 12:29 PM
Hi, I followed the tutorial to implement Gravatars in my blog, and it worked perfect, but, I can’t align the image to the text.
I want it to display aligned with the author name and date and the image to link the author’s site.
Here’s how it looks now on my blog:
http://www.geekspot.com.ar/geekspot/anunciate-gratis-en-geekspot/#comments
Do I need some CSS? I’ve got this
ol.commentlist h5{ /*nick*/
display:inline;
font-size:11px;
margin:0;
padding:0;
}
ol.commentlist h5 a:hover{
border-bottom:1px solid #ccc; color:#185DA0; background-color:inherit;
}
ol.commentlist h5 small{
display:inline;
}
March 30th, 2008 at 12:33 PM
It looks like you have the Gravatar in the H5. Take it out, and put it above the H5 (and the small tag). Then just float it to the left, add padding to the right and it should be fine.
March 30th, 2008 at 7:30 PM
Thanks for the answer, what I don’t understand is in the CSS I have to create an ol or something?
I’ve already took it out.
ol.commentlist .gravatar{
float: left;
border-width: 1px;
border-style: solid;
padding: 2px;
}
March 30th, 2008 at 8:06 PM
The OL is the list that all the comments are contained in.
Try putting a DIV around the H5 and the small tag, so they are grouped together.
March 31st, 2008 at 4:43 PM
Well, I tried a lot of things that didn’t work, and looking the CSS of some blogs I read daily I found this piece of code wich did the trick:
.commentlist img {
float: left;
background: #fff;
padding: 3px;
border: 1px solid #e3e3e3;
margin-right: 5px;
}
March 31st, 2008 at 6:45 PM
Yeah, that’s what I was getting at. Float it out to the left and add space to its right. Good to see you got it worked out, looks good.
April 2nd, 2008 at 2:46 AM
Good Work! TANKS
April 2nd, 2008 at 5:30 AM
Thanks for your help!
April 3rd, 2008 at 10:17 PM
THANKS FOR THE WORK!
I have a question: If I want to put this gravtars in my “author’s archive” (I have more then 1 author in my blog). how do I do that with the gravtars.com.
I’m using WP 2.5.
Thanks…
April 7th, 2008 at 5:23 PM
cool info
April 8th, 2008 at 1:21 AM
Thanks too much for your excellent way!!!
April 12th, 2008 at 1:15 AM
This is awesome, I create custom themes for my clients and I am sure this will really help as I don’t have to tell them to install the plug-ins.
Thanks for Sharing!
April 12th, 2008 at 10:49 AM
My Gravatar NEVER shows up in any site!! What could be wrong? I’ve signed up, added a 80×80 avatar too. ut jus could not get it to work!
April 15th, 2008 at 11:58 AM
Thanks for this tutorial. Great job……!
April 15th, 2008 at 7:28 PM
Nice plugin
April 16th, 2008 at 5:59 AM
Nice articles! Thanks for this tutorial..
April 16th, 2008 at 6:23 AM
checking it out
April 17th, 2008 at 4:45 PM
Hi Conner,
I also got the script working perfectly first time (WP2.5) - thanks so much.
Like some others I’m struggling with styling; perhaps you or another reader can help me out.
When comments are short — like the the third and fourth ones here: http://www.indietravelpodcast.com/podcast/053-the-camino-de-santiago-de-compostela/ — I cannot get the gravatar to “push” the light-grey div down further, so I’m getting some overlap between comments and gravatars.
I have experimented with several margin/padding CSS solutions, but have nad no success. Any help appreciated.
April 22nd, 2008 at 6:29 AM
Man.. i wish it would work for me… i am trying each and everything from past 5 hours… I hope ur trick works!
April 22nd, 2008 at 4:54 PM
I’ll be trying it this weekend during my holiday. Hope it works. I tried it before, but then the text got pushed down instead of floating next to the gravatar. I’m gonna try and get it right this time!!!!
April 23rd, 2008 at 9:38 AM
This is really simple and well done. Good job.
April 27th, 2008 at 10:21 PM
Yah, I like it but I am a little illiterate when it comes to technical work, gravatars. I always use plugins that work themselves. Boy! I love the new wordpress 2.5.1 it is so easy to manage.
April 30th, 2008 at 8:20 AM
Thank you guys thats a really simple way to do it and it really works for me. Thanks once again
April 30th, 2008 at 2:44 PM
This is awesome, I create custom themes for my clients and I am sure this will really help as I don’t have to tell them to install the plug-ins.
Leave a Reply
32 Trackbacks