{"id":5708,"date":"2011-03-27T07:06:48","date_gmt":"2011-03-27T12:06:48","guid":{"rendered":"http:\/\/www.allyngibson.net\/?p=5708"},"modified":"2011-03-27T07:06:48","modified_gmt":"2011-03-27T12:06:48","slug":"on-allyns-adventures-with-unicorns","status":"publish","type":"post","link":"http:\/\/www.allyngibson.com\/?p=5708","title":{"rendered":"On Allyn&#8217;s Adventures With Unicorns"},"content":{"rendered":"<p>It all began last year, on April Fool&#8217;s Day.<\/p>\n<p>As a longtime WordPress user, I frequently visit <a href=\"http:\/\/planet.wordpress.org\/\">Planet WordPress<\/a>, an aggregation blog of a number of top WordPress-specific blogs.<\/p>\n<p>And on April Fool&#8217;s Day, there was a post from the Gravatar blog on a new gravatar algorithm &mdash; <a href=\"http:\/\/blog.gravatar.com\/2010\/04\/01\/unicorn-gravatars-stack-overflow\/\">Unicorns<\/a>.<\/p>\n<p>A gravatar, for those unfamiliar with the term, is a little graphic that represents the author of a comment, provided by <a href=\"http:\/\/en.gravatar.com\/\">gravatar.com<\/a>.  It&#8217;s a free service; you set up an account, and wherever you post on the &#8216;net that uses the Gravatar service (as many WordPress-powered sites do), you maintain a consistent visual identity.  And for people who haven&#8217;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.<\/p>\n<p>And now, there was a new service &mdash; <a href=\"http:\/\/unicornify.appspot.com\/\">Unicornify!<\/a>.  People could have sparkles and unicorns and rainbows as their avatars.<\/p>\n<p>Naturally, since it was April Fool&#8217;s Day, of course. \ud83d\ude42<\/p>\n<p>(If you want to see what your Unicorn avatar would look like, <a href=\"http:\/\/unicornify.appspot.com\/use-it\">click here<\/a> for the test page.)<\/p>\n<p>WordPress guru <a href=\"http:\/\/ottopress.com\/\">Otto42<\/a> wrote a plugin that took advantage of the unicorn gravatar service, <a href=\"http:\/\/ottopress.com\/wordpress-plugins\/unicornify\/\">Unicornify<\/a>.<\/p>\n<p>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.<\/p>\n<p>But I noticed something.<\/p>\n<p>Because of the way the plugin worked, if someone had a registered gravatar, they wouldn&#8217;t see it.  They would see, instead, the Unicorn gravatar.<\/p>\n<p>Well!  I decided that simply wouldn&#8217;t do.  I don&#8217;t want to lose <a href=\"http:\/\/www.allyngibson.com\/?p=1489\">my Charles Schulz-esque Fourth Doctor gravatar<\/a> on my own website, after all.  And so I deactivated the plugin.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.allyngibson.com\/graphics\/allyn-unicorn.jpg\" height=\"128\" width=\"128\" align=\"right\" class=\"alignleft\" alt=\"Allyn's Unicorn Avatar\" \/>That said, I had a pretty awesome unicorn gravatar, if I do say so myself.  Rainbows!<\/p>\n<p>Last weekend, I did some clean-up on my WordPress install, deleting some old plugins that I no longer needed.<\/p>\n<p>I&#8217;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 &mdash; generate a unicorn avatar for people without registered gravatars, but use the registered gravatar for those who have them.<\/p>\n<p>I couldn&#8217;t see why this wouldn&#8217;t be doable.<\/p>\n<p>I studied up on the WordPress codex; the <a href=\"http:\/\/codex.wordpress.org\/Using_Gravatars\">Using Gravatars<\/a> article had some code for testing whether or not a gravatar is registered with the Gravatar service that would be useful:<\/p>\n<p><code>function validate_gravatar($email) {<br \/>\n&nbsp; &nbsp; &nbsp;\/\/ Craft a potential url and test its headers<br \/>\n&nbsp; &nbsp; &nbsp;$hash = md5($email);<br \/>\n&nbsp; &nbsp; &nbsp;$uri = 'http:\/\/www.gravatar.com\/avatar\/' . $hash . '?d=404';<br \/>\n&nbsp; &nbsp; &nbsp;$headers = @get_headers($uri);<br \/>\n&nbsp; &nbsp; &nbsp;if (!preg_match(\"|200|\", $headers[0])) {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;\t$has_valid_avatar = FALSE;<br \/>\n&nbsp; &nbsp; &nbsp;} else {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$has_valid_avatar = TRUE;<br \/>\n&nbsp; &nbsp; &nbsp;}<br \/>\n&nbsp; &nbsp; &nbsp;return $has_valid_avatar;<br \/>\n}<\/code><\/p>\n<p>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 &mdash; the registered gravatar or the unicorn gravatar.<\/p>\n<p>Genius!<\/p>\n<p>So, I dropped that validate_gravatar function into the plugin, then wrote some code.  Instead of the line:<\/p>\n<p><code>&nbsp; &nbsp; &nbsp;$host = 'http:\/\/unicornify.appspot.com';<\/code><\/p>\n<p>I would do this:<\/p>\n<p><code>&nbsp; &nbsp; &nbsp;$registered_gravatar = validate_gravatar ( $email );<\/p>\n<p>&nbsp; &nbsp; &nbsp;if ( $registered_gravatar ) {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$host = 'http:\/\/www.gravatar.com';<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$alt = 'Registered Gravatar';<br \/>\n&nbsp; &nbsp; &nbsp;} else {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$host = 'http:\/\/unicornify.appspot.com';<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$alt = 'Unicorn Gravatar';<br \/>\n&nbsp; &nbsp; &nbsp;}<\/code><\/p>\n<p>As genius as this was&#8230;<\/p>\n<p>It didn&#8217;t work.<\/p>\n<p>It <i>should<\/i> have worked.  But some PHP config settings in my webserver were preventing it from working, and since I couldn&#8217;t change those, I wondered if I might be able to find a workaround.<\/p>\n<p>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.<\/p>\n<p>So, I turned to the source of all knowledge &mdash; the Internet. \ud83d\ude09<\/p>\n<p>And, at php.net, I found a workaround.  In <a href=\"http:\/\/php.net\/manual\/en\/function.get-headers.php\">their article on the get_headers function<\/a>, 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.<\/p>\n<p>Lo and behold, it worked!<\/p>\n<p>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:<\/p>\n<p><code>&nbsp; &nbsp; &nbsp;$uri = 'http:\/\/www.gravatar.com\/avatar\/' . $email_hash . '?d=404';<br \/>\n&nbsp; &nbsp; &nbsp;$headers = @get_headers($uri);<br \/>\n&nbsp; &nbsp; &nbsp;if (!preg_match(\"|200|\", $headers[0])) {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$host = 'http:\/\/unicornify.appspot.com';<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$alt = 'Unicorn Gravatar';<br \/>\n&nbsp; &nbsp; &nbsp;} else {<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$host = 'http:\/\/www.gravatar.com';<br \/>\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$alt = 'Registered Gravatar';<br \/>\n&nbsp; &nbsp; &nbsp;}<\/code><\/p>\n<p>Until I get tired of it then &mdash; probably by Mid-Year&#8217;s Day &mdash; I&#8217;ll have unicorn gravatars for people who leave comments!  Leave a comment and you&#8217;ll see your own colorful unicorn. \ud83d\ude42<\/p>\n<p>For WordPress junkies who want the complete code, <a href=\"http:\/\/www.allyngibson.com\/unicornify.zip\">here&#8217;s the modified plugin<\/a>.  Use <s>at<\/s> for your own <s>peril<\/s> fun! :h2g2:<\/p>\n<p>Sparkles!  Rainbows!  Unicorns!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It all began last year, on April Fool&#8217;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&#8217;s Day, there was a post from the Gravatar blog on a new gravatar algorithm &mdash; Unicorns. A gravatar, for those unfamiliar with<a class=\"more-link\" href=\"http:\/\/www.allyngibson.com\/?p=5708\">Continue reading <span class=\"screen-reader-text\">&#8220;On Allyn&#8217;s Adventures With Unicorns&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4095],"tags":[58,759,4096],"class_list":["post-5708","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-php","tag-plugin","tag-wordpress","entry"],"_links":{"self":[{"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=\/wp\/v2\/posts\/5708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5708"}],"version-history":[{"count":0,"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=\/wp\/v2\/posts\/5708\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5708"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.allyngibson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}