Replacing plugins with template code: Gravatars

Lately I've been trying to reduce the number of plugins I've got loading to speed up my WordPress a bit, especially those plugins that do "stuff" in my theme. I've been replacing these plugins with template code, and this is the easiest fix I had so far: I replaced a 40k gravatar plugin with one (!!) line of code which generates all the images:

<img class="gravatar"
alt="gravatar"
src="http://www.gravatar.com/avatar.php?gravatar_id=<?php echo md5($comment-/>comment_author_email); ?>&rating=R&size=80&default=http%3A%2F%2Fyoast.com%2Fimages%2Fgravatar.png"/>

The source URL there needs some explanation:

  • the gravatar id is an MD5 hash comment author's email address
  • the rating can be G | PG | R | X
  • the default is the URL to my default image, instead of the blue gravatar image

Now my gravatars plugin did some server-side caching, but since Automattic acquired Gravatar a while back, they're now super fast, and all that caching can just slow your site down, because you can't beat their performance anyway.

Tags: ,
Category: WordPress
You can skip to the end and leave a response.

13 Responses to “Replacing plugins with template code: Gravatars

That's great! I'm going to switch that over on my sites now :) Thanks

Will this change next week when WP 2.5 is released? I thought they built gravatars into the new release, but I haven't paid a lot of attention to the details.

@Dazzlindonna yes they did, but that will probably do something similar to this :)

Did you just do that overnight? For some reason my gravatar wasn't showing on your comments yesterday, but it's showing today. weird...

Anyway, thanks for another useful tip!

Nice hack! I'm going to try to do something like this right away on my current theme.

Joost, you can also use for <?php echo the shorter version: comment_author_email

Are your sure? Ik think it must be

$comment->comment_author_email

Hmm, the short tag was replaced.

This one: < ? = (without spaces)

@Henri it says $comment->comment_author_email ;)

Very interesting article! Keep up the good work.

Love it?
With WP 2.5 correct me if im wrong but does this mean we wont need the Gravatar Plugin anymore??

its all about the speed.

Great stuff Joost

Nice one Joost! Still i'm just gonna wait for the WP 2.5 ;)!

It's not so usefull change.
You've replaced local gravatar reference (possibly cached by plugin like Gravatars2) with remote call to gravatar.com site. Thus, your site now is fully dependable on gravatar.com, which sucks very often. I'd never make such a change. Vice versa, I am trying to use local cache where is possible.
IMHO.

Comments closed, if you feel you have something to say:
drop me a line.