Replacing plugins with template code: Gravatars

March 13th, 2008 – 13 Comments

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.


13 Responses to Replacing plugins with template code: Gravatars

  1. Alex
    By Alex on 31 March, 2008

    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.

  2. Roelof
    By Roelof on 27 March, 2008

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

  3. Drew Stauffer
    By Drew Stauffer on 14 March, 2008

    its all about the speed.

    Great stuff Joost

  4. Jermayn Parker
    By Jermayn Parker on 14 March, 2008

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

  5. Domain Registrar
    By Domain Registrar on 13 March, 2008

    Very interesting article! Keep up the good work.

  6. Joost de Valk
    By Joost de Valk on 13 March, 2008

    @Henri it says $comment->comment_author_email ;)

  7. Henri
    By Henri on 13 March, 2008

    Hmm, the short tag was replaced.

    This one: < ? = (without spaces)

  8. Henri
    By Henri on 13 March, 2008

    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

  9. Scott Johnson
    By Scott Johnson on 13 March, 2008

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

  10. seosurvivor
    By seosurvivor on 13 March, 2008

    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!

  11. Joost de Valk
    By Joost de Valk on 13 March, 2008

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

  12. DazzlinDonna
    By DazzlinDonna on 13 March, 2008

    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.

  13. Adam Dempsey
    By Adam Dempsey on 13 March, 2008

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


Check out our must read articles about Analytics