Girl Meets Geekdom

Alive and Kicking!

Who Needs a Plugin for Gravatars?

Sunday, December 11th, 2005 at 3:45 am

Today I got all excited about Gravatars and downloaded a fancy plugin from the WP Plugins DB and proceeded to activate my plugin hoping to soon see pretty little pictures next to the comments in my posts. To my dismay, this did not happen. It was not some magical plug in as I had hoped. I had to go to the downloader’s site to insert a massive if statement in the foreach loop of my comment block.

So, I figured, fine, I’m not afraid of code. So I inserted it, edited my stylesheet and even created a little icon for those “gravatarless” individuals. To my dismay, this resulted in everyone being “gravatarless.” Needless to say, I was frustrated. In order to install this fancy plugin, I had to drop in a gravatar folder containing default images as well as a rather large .php file. I opened it up to find that there was actually a lot of code in there and automatically assumed that it’s probably not something I could dissect in a few hours. So I proceeded to the Gravatar website to figure out if they had any solutions.

What I found there was shocking. So this thing that apparently required a plug in consisting of a folder and a .php file with a LOT of functions is really something that can be done in as many lines of code as I had to add to my comments.php file. Even LESS if I wanted the gravatars to always link back to the gravatar website. No plugins required.

So for those of you who use WordPress and are too lazy to install the plugin here’s the simplest form of the code:


<img src=" http://www.gravatar.com/avatar.php?gravatar_id=
  <?php echo md5($comment->comment_author_email)
  .”&default=”.urlencode($default); ?>” alt=”" />

That code displays just the image. Of course I use a much more complicated code to generate the links and default image:


<?php foreach ($comments as $comment) :
$default=””;
if (” != get_comment_author_url()) {
   echo “<a href=’$comment->comment_author_url’
title=’Visit $comment->comment_author’>”;
} else {
   echo “<a href=’http://www.gravatar.com’
title=’Create your own gravatar at gravatar.com!’>”;
}
echo “<img src=’”;
if (” == $comment->comment_type) {
  echo “http://www.gravatar.com/avatar.php?gravatar_id=”
  .md5($comment->comment_author_email)
  .”&default=”.urlencode($default);
} elseif ( (’trackback’ == $comment->comment_type) ||
  (’pingback’ == $comment->comment_type) ) {
  echo gravatar($comment->comment_author_url);
}
echo “‘ alt=” class=’gravatar’ width=’80′ height=’80′ /></a>”; ?>

Just enter in the full path of your default image where it says $default = “” and replace the


<?php foreach ($comments as $comment) ?>

Anyway…I figured if you can accomplish it in the same amount of code in your .php file, why the hell do you need a plugin with a lot of functions to do it for you?

2 Responses to “Who Needs a Plugin for Gravatars?”

Man, reading this post just makes me SOOOO glad that we went with .NET over PHP. I’m totally a Microsoft fanboy now, but how can I not be when they have such great tech like .NET and the 360??

Gravatars, you will find, are quite fickle sometimes. The domain has expired on at least one occassion, causing all blogs using them to die until it came back up, or rather, until we just switched to using an IP…


Got something to say?