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, last.fm 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 Last.fm 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.
1 2 3 4 |
define('MAGPIE_CACHE_ON', 1); //2.7 Cache Bug define('MAGPIE_CACHE_AGE', 800); define('MAGPIE_INPUT_ENCODING', 'UTF-8'); define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function twitter_messages($username = 'douglasradburn') { include_once(ABSPATH . WPINC . '/rss.php'); $feed = fetch_feed('http://twitter.com/statuses/user_timeline/'.$username.'.rss'); $i=0; foreach($feed->get_items() as $item){ if($i < 1){ $description = trim(str_replace($username . ': ', '', $item->get_description())); echo twitterify(strip_tags($description)); $i++; } } } |
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:
1 |
$description = trim(str_replace($username . ': ', '', $item->get_description())); |
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.
1 |
twitter_messages(); |
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.
1 2 3 4 5 6 7 8 |
function twitterify($ret) { # thanks to http://www.snipe.net/2009/09/php-twitter-clickable-links/#axzz0psFrJPHB $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2", $ret); $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret); $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret); return $ret; } |
Image Credit: ::: M @ X :::
Pingback: Tweets that mention New blog posting, How to get a twitter feed into your Wordpress theme - -- Topsy.com