Updated: Stack Overflow WordPress Widget

Introduction

Just a quick post to say I’ve updated my Stack Overflow WordPress Widget to implement some basic caching. I’m still in the process of converting it to the new JSON service that SO have provided; but in the meantime this should cut down on the amount of traffic it uses.

You should be able to upgrade straight from the admin section of your blog; but you can also grab it from the WordPress Plugin Directory.

The cache time is configurable in the widget settings, and defaults to 30minutes. It attempts to locate your temp directory, then creates a unique filename cache in there; so there’s no need to chmod a cache file to get it to work.

I’ll hopefully get a JSON version working in the next few days, but if anyone has any problems feel free to give me a shout from the contact page.

Update: It seems the JSON version was easier than I thought 🙂 I’ve uploaded v2.0.0 that still includes the caching changes, but also uses the “official” JSON feed. If you have already used the cached version you might might need to delete the cache file to make it re-download the raw data.

The only “downside” to the JSON version is it requires json_decode, so you must have PHP 5.2.0 or above. If you can’t satisfy this requirement then you’ll be better off with the previous version.

Stack Overflow WordPress Widget

If you haven’t already, you should really check out Stack Overflow; it’s a community driven programming questions/answers site where people earn reputation and badges for contributing.

I wanted to put an XBox Live style "gamertag" on my blog with a link to my Stack Overflow profile and my current "reputation". I couldn’t find anything out there so I thought I’d dig up some Widget sample code and steal adapt it to display what I wanted.

There doesn’t seem to be any API for getting the information I wanted, so it uses a basic screen scrape, which may well stop working if they tweak the pages and may well not work for everyone’s profile page, but it works for mine and that’s all I care about 😉 Seriously though, if anyone else wants to use it and has problems, feel free to drop me a line and I’ll take a look at it. I’m no PHP expert either, so if anyone wants to tell me if my code sucks horribly, feel free 😀

You should be able to see the widget over on the right, and you can grab the widget itself below. Once it’s installed you need to configure it to give it your user number (26507 in my case) and your email address (so it can pull out your Gravatar). The email address isn’t used for anything other than hashing for the Gravatar, but you can read the source if you don’t believe me 🙂

Update: I’ve made a few changes in response to a few comments, and also removed the little place holder and badges in there.  It takes into account badges that have been awarded multiple times, so it *should* be the same as your SO page 🙂

Update2: Thanks to Ólafur Waage for providing an update to add a clear div after the widget and to run the name through htmlentities to handle names with “funky characters” 🙂

Update3: Version 2.0.0 is now available on the WordPress plugin page. It has a simple cachine system and also now pulls its data from the Stack Overflow JSON feed, rather than using screen scraping. More info in the new post: Updated: Stack Overflow WordPress Widget.

New screenshot and download below:

Stack Overflow Widget

sowidget.zip