On Allyn’s Adventures With Unicorns

It all began last year, on April Fool’s Day.

As a longtime WordPress user, I frequently visit Planet WordPress, an aggregation blog of a number of top WordPress-specific blogs.

And on April Fool’s Day, there was a post from the Gravatar blog on a new gravatar algorithm — Unicorns.

A gravatar, for those unfamiliar with the term, is a little graphic that represents the author of a comment, provided by gravatar.com. It’s a free service; you set up an account, and wherever you post on the ‘net that uses the Gravatar service (as many WordPress-powered sites do), you maintain a consistent visual identity. And for people who haven’t signed up for the service, there are plugins that produce something other than a default image, keyed to the e-mail address. I used one that made goofy faces called Wavatar.

And now, there was a new service — Unicornify!. People could have sparkles and unicorns and rainbows as their avatars.

Naturally, since it was April Fool’s Day, of course. 🙂

(If you want to see what your Unicorn avatar would look like, click here for the test page.)

WordPress guru Otto42 wrote a plugin that took advantage of the unicorn gravatar service, Unicornify.

Since I like to tinker with my WordPress install, I downloaded the Unicornify plugin and installed it. It works by tapping into a WordPress hook; when a gravatar is called, instead of using the Gravatar service, the gravatar will be generated from the Unicornify service.

But I noticed something.

Because of the way the plugin worked, if someone had a registered gravatar, they wouldn’t see it. They would see, instead, the Unicorn gravatar.

Well! I decided that simply wouldn’t do. I don’t want to lose my Charles Schulz-esque Fourth Doctor gravatar on my own website, after all. And so I deactivated the plugin.

Allyn's Unicorn AvatarThat said, I had a pretty awesome unicorn gravatar, if I do say so myself. Rainbows!

Last weekend, I did some clean-up on my WordPress install, deleting some old plugins that I no longer needed.

I’d forgotten about Unicornify. I activated it, just for kicks, and then I wondered if, perhaps, I could make the plugin work the way I wanted it to work — generate a unicorn avatar for people without registered gravatars, but use the registered gravatar for those who have them.

I couldn’t see why this wouldn’t be doable.

I studied up on the WordPress codex; the Using Gravatars article had some code for testing whether or not a gravatar is registered with the Gravatar service that would be useful:

function validate_gravatar($email) {
     // Craft a potential url and test its headers
     $hash = md5($email);
     $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
     $headers = @get_headers($uri);
     if (!preg_match("|200|", $headers[0])) {
           $has_valid_avatar = FALSE;
     } else {
          $has_valid_avatar = TRUE;
     }
     return $has_valid_avatar;
}

The Unicornify plugin builds a URL to http://unicornify.appspot.com, and the Unicorn avatar link is built in exactly the same way that a link to gravatar.com is built. It occurred to me that if I tested the e-mail address that is passed through Unicornify to see whether or not a gravatar linked to that account exists, then I could determine which gravatar to call — the registered gravatar or the unicorn gravatar.

Genius!

So, I dropped that validate_gravatar function into the plugin, then wrote some code. Instead of the line:

     $host = 'http://unicornify.appspot.com';

I would do this:

     $registered_gravatar = validate_gravatar ( $email );

     if ( $registered_gravatar ) {
          $host = 'http://www.gravatar.com';
          $alt = 'Registered Gravatar';
     } else {
          $host = 'http://unicornify.appspot.com';
          $alt = 'Unicorn Gravatar';
     }

As genius as this was…

It didn’t work.

It should have worked. But some PHP config settings in my webserver were preventing it from working, and since I couldn’t change those, I wondered if I might be able to find a workaround.

The problem, as it turns out, was the get_headers function. It simply would not work with my server, probably due to the way that my webserver was upgraded from PHP4 to PHP5.

So, I turned to the source of all knowledge — the Internet. 😉

And, at php.net, I found a workaround. In their article on the get_headers function, there were several workaround functions posted for pre-PHP5 servers. My server, I should note, is, in fact, a PHP5 server. But I thought the workaround might be a good solution, and so I dropped the code for one of those workarounds into the Unicornify plugin.

Lo and behold, it worked!

I made another change to the code. I eliminated the validate_gravatar function entirely, and instead integrated it into the unicornify function. There was no need to hash the e-mail address a second time; the Unicornify code already did that. The result was some simpler, less redundant code, and the result was this:

     $uri = 'http://www.gravatar.com/avatar/' . $email_hash . '?d=404';
     $headers = @get_headers($uri);
     if (!preg_match("|200|", $headers[0])) {
          $host = 'http://unicornify.appspot.com';
          $alt = 'Unicorn Gravatar';
     } else {
          $host = 'http://www.gravatar.com';
          $alt = 'Registered Gravatar';
     }

Until I get tired of it then — probably by Mid-Year’s Day — I’ll have unicorn gravatars for people who leave comments! Leave a comment and you’ll see your own colorful unicorn. 🙂

For WordPress junkies who want the complete code, here’s the modified plugin. Use at for your own peril fun! :h2g2:

Sparkles! Rainbows! Unicorns!

Published by Allyn

A writer, editor, journalist, sometimes coder, occasional historian, and all-around scholar, Allyn Gibson is the writer for Diamond Comic Distributors' monthly PREVIEWS catalog, used by comic book shops and throughout the comics industry, and the editor for its monthly order forms. In his over ten years in the industry, Allyn has interviewed comics creators and pop culture celebrities, covered conventions, analyzed industry revenue trends, and written copy for comics, toys, and other pop culture merchandise. Allyn is also known for his short fiction (including the Star Trek story "Make-Believe,"the Doctor Who short story "The Spindle of Necessity," and the ReDeus story "The Ginger Kid"). Allyn has been blogging regularly with WordPress since 2004.

3 thoughts on “On Allyn’s Adventures With Unicorns

Leave a Reply

Your email address will not be published. Required fields are marked *