blog.thoughtwax.com

A more reliable Twitter badge

If God didn’t want us to add widgets to our websites he wouldn’t have created sidebars.

I added a Javascript call to the Twitter API to display my latest update on this site a while ago, but it slowed page load down quite a bit, so I removed it again (at least until Twitter get their scaling problems under control). I could have written some server side code to cache my latest update, but it didn’t seem worth the effort.

Then Google released the very cool AJAX Feed API this week, and I stayed up for hours playing with it. One of the many ways you can use the API is to hit Google’s more reliable servers for the latest cached version of a feed (from the same data source that Google Reader uses). So instead of accessing your feed directly, pipe it through Google and call it easily with Javascript.

To display your latest Twitter status, add these lines between your web page’s head tags:

<script type="text/javascript">
  var THWX_twitter_id = YOUR_TWITTER_ID_NUMBER;
</script>
<script type="text/javascript" src="http://www.google.com/jsapi?key=YOUR_API_KEY"></script>
<script type="text/javascript" src="http://thoughtwax.googlepages.com/twitter.js"></script>

Replace YOUR_TWITTER_ID_NUMBER with your Twitter feed ID (the number in the URL of your Twitter RSS feed) and replace YOUR_API_KEY with your Google Ajax Feeds API key, which you can get here.

Then add this HTML where you want your latest update to appear:

<div id="THWX_twitter_status"></div>

Lovely. Example here.

4 comments

  1. Looks good. I’ve been using an RSS plugin (http://rawlinson.us/blog/?p=212) to scrape my latest twits. Would be interested in your thoughts re. pros and cons of one over the other.

    Posted by Rob, April 24th, 2007 at 10:34 pm

  2. I’d say stick with what you’ve got if it isn’t giving you any problems.

    The main advantage of my script is that it doesn’t rely on a database, server-side scripting or crons, but you’re getting everything for free with your Wordpress plugin anyway.

    Posted by emmetc, April 25th, 2007 at 10:43 am

  3. im stupid,
    i dont understand anything, please excuse my ignoramius
    what exactly is this?

    Posted by cnnr, April 27th, 2007 at 7:50 pm

  4. Hi cnnr. So twitter.com is a website where you can post tiny text updates about what you are doing in a kind of a mini-blog, your inner monologue put on the internet. And your friends can add you as their friends, like the kids do on myspace, and they can see that, for example, you’re drinking green tea and reading your email RIGHT NOW!

    And somehow people like it. And then the thing that I wrote is a bit of code to put the latest update that you wrote on your website or blog or whatever.

    Hope NY is fun.

    Posted by emmetc, May 3rd, 2007 at 7:21 pm

Leave a comment