I wanted to add a recent tracks on Last.fm widget to my blog. The caveat is that I use wp-cache, meaning, a standard RSS feed widget would be often out of date. I opted for a JSONP widget, the implementation is fairly straight forward. After signing up for a Last.fm API account, I hooked into the recent tracks API, which is capable of providing a JSONP response.
I ended up using jQuery, since it comes with Wordpress. Though a library isn’t entirely necessary for such a simple widget, it’s already on the page, so why not use it. Also, it gave me a good reason to play around and test out that library. For the most part, jQuery is like all the other mainstream libraries in terms of syntax. The no-conflict mode is very nice for pages where name space collision might be an issue, but it did have a few odd behaviors that I’m not sure I understand. One that really confused me was jQuery’s getElementById() equivalent.
var elem = jQuery('#my_id')
For some reason, this returns an array. To actually access the element above, you need to say elem[0]. By definition getElementById() returns only one element. There is probably a good reason for this (at least I hope so), but I’m not sure why yet.
Take a look at my implementation in you’re interested. Help yourself to the code, but please get your own api key. It’s not much more than a simple for-loop that creates a handful of elements. I could probably make it more plug-and-play, but it suits my needs for now.
Update: I have turned this code into a full fledged Wordpress Plugin / Widget. You can download the plugin here. Perfect for both widget-enabled themes and non-widget-enabled. Themes. Get it now.