¶ The great thing about publishing a blog through your own CMS is that said CMS can be hacked around and added to at will. Of course the downside is that you have to write the code yourself. Unless someone else decides to publish some useful code for the taking. You’ll find below a snippet of code shows how I’ve implemented gravatars in PHP. How useful you’ll find it is open to question.
Essentially the code displays an image only if a gravatar is available. I didn’t want to display a default image so I check that the returned gravatar has a width greater than 1. If the commenter has also supplied a URL then the gravatar is made to link to it.
= "";
if () {
= "http://www.gravatar.com/avatar.php?
gravatar_id=".md5().
"&size=40&rating=PG";
= @getimagesize();
= ;
if ( > 1) {
= "<img src='' alt='".
.
"’s Gravatar' title='"..
"’s Gravatar' class='gravatar' />";
if () {
= "<a href='".."'>".
."</a>";
}
}
}
It’s pretty straight forward so I’ve left it uncommented. If you need anything explained, just ask.





Comments
1
Why do you have to check the image at all?
If you don’t send a default image URL to the Gravatar server, then you don’t get a default image. All you’ll get back is the URL to a 1×1 pixel transparent gif.
2
True, Jonathan, but I don’t want to display the single pixel gif either (cos it will get a link and border around it).
3
Oh I see.
Just to clarify – this is because you are using a custom border. If you use Gravatar’s border system the 1×1 pixel is displayed without a border. This is how I have it implemented on my website.
4
Psssst, Richard, before anybody notices: you’ve declared a variable that doesn’t exist:
I think instead of “
” between the link tag, you mean “” ?
Neat code, though, I suppose if you wanted to display your own default image, rather the Gravatar default, you could add an “else” condition after the “if ( > 1)” block, and insert your own image.
5
I don’t think you need to use the HTML entity for the ampersand when fetching the image size via getimagesize(). Later on when you output it, sure.
6
Louise – thanks for that. Duly corrected. That’s what happens when you try to tiny up your code for public consumption!
Justin – yeah you’re right, but it seems to work OK so I’ll let it be (no doubt leaving problems for myself somewhere down the line).
7
Doesn’t this reduce page loading times? I mean, on post with more than two comments?
It sure took a while to load this page – but that might just be my bad.
8
Copycat!
;o)
Although yours is much better explained than mine.
9
What about spoofing?
If you know someone’s email what’s to stop you pretending to be them? Or am I missing something?
10
Testing to see if my Gravatar works here.
11
Hmmm, it’s now a problem with my Gravatar account… OK so that means my WordPress nightly is broken somewhere. Anyway, thanks for providing the boilerplate code Rich!
12
Goynang: yeah, and why not post comments under other people’s names? We all know it ain’t hard to pretend to be someone else on the web – Gravatar-wise or not…
13
how do I make my gravatar show up?
14
Just testing. :/
15
Using this script increases page loading times a lot, especially on pages with lots of comments. Of course it does – for a post with 30 replies, for instance, the script would check 30 off-site images! All this takes a while.
16
Mathias – It’s true this script will increase page loading times for the reasons you state, however Gravatars per se wouldn’t necessarily increase page rendering time.
The script I presented could (and probably should) be optimised so it only checks for a Gravatar every now and then, not for each comment each time the page is displayed.
Add your comment
Comments are now closed on this post. If you have more to say please contact me directly.