Skip to content

Using Magpie within WordPress for RSS parsing

Wordpress logo

It can be a real mission getting RSS feeds perfect, just the way you want them within your WordPress site. People normally go for plug-ins, jQuery or custom PHP to try and create all manner of effects but these all have their respective issues.

Instead, let me show you how easy it is to parse a Twitter feed using Magpie which is built into WordPress.

First things first, find your theme’s directory and insert this code:

include_once(ABSPATH . WPINC . '/feed.php');
$username='twittername'; // set user name
$format='xml'; // set format

$feed = fetch_feed("http://api.twitter.com/1/statuses/user_timeline/{username}.{format}");
$maxitems = 5;

echo("<ul>");			

if ($feed == ""){
	$items = array_slice($feed->items, 0, $maxitems);

	if(!empty($items)) {
		foreach ($items as $item) {
			echo("<li>".$item->text."</li>");
		}
	}
} else {
	echo("<li>No tweets at present.</li>");
}

echo("<ul>");

If you’re looking to sort out some particularly irritating caching issues, try putting these values into your wp-config.php file in the root of your site:

define('MAGPIE_CACHE_ON', 0); // deactivate cache
define('MAGPIE_CACHE_AGE', 60*60) // Cache 1 hour in seconds

Enjoy…

Leave a Reply

You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS