Since Twitter changed things, you’re probably better off looking at A WordPress Twitter stream, without the RSS…



WordPress comes with a version of Magpie RSS as part of its base installation.

This means you can easily retrieve outside sources (eg: twitter, flickr, and many others) on your blog via a syndication system, such as rss or atom.

Magpie RSS also comes with a handy caching system. This means that you don’t have to continually hammer someone else’s system and you can cache the data within WordPress. Twitter has implemented API request throttling. Request throttling, is often used as a method to ensure Quality of Service for all sorts of systems to reduce load for network and application uses. It is used by Twitter so as to not overwhelm the system – we’ve all met the Fail Whale.

By default, Magpie has a 1 hour time out. This is great for most systems, but those like Twitter and are updated with greater frequency and therefore, you don’t want to cache them for as long. Having said that, caching is still a great idea, so that Twitter isn’t hit every time a user requests a page.

Think of it this way – if there is no caching and 5000 visitors visit your site at one time, then 5000 requests would be made to Twitter. With caching in place, 1 request would need to be made – and then a second wouldn’t need to be made for however long the caching was in place for.

You can manipulate WordPress’ caching system with the following commands.

Once we’ve got Magpie set up, we can go about pulling out your twitter feed. To do this, we use the WordPress RSS tools like we mentioned above. We include that library (using include_once incase the file has already been included somewhere else when you include this).

Simply, we fetch the RSS feed, then we foreach over each one and echo them out. Our $i variable can be used to limit the number of results we want to work with, and of course, you could return the data rather than actually processing it within the function. In this example, we simply use the description, but we could quite easily use ->get_date(‘d.m.y H:i:s’) to grab the time and date of the tweet.

All the tweets that come out of the RSS feed include your username, so we remove this using the above code, highlighted here:

To call the function, it takes on parameter, the Twitter ID you need. In this function, we set PHP to have a default, as it’s rare we might be pulling in more than one feed, so it makes life a bit easier.

Many people have built great Twitter plugins that do all sorts of things but the only additional function I use is to create links from hash tags, links and @ replies within the tweets.

Image Credit: ::: M @ X :::