So you want to get a RSS feed and echo it to a WordPress page or sidebar – you’ve already got the RSS sidebar widget feature, but what if you want a little more control over what’s coming out?
New in WordPress 2.8 is the fetch_feed() function – as the name suggests, a means of retrieving an external RSS feed, parsing it (extracting the relevant info, for output purposes) and also caching the raw data to ease the strain on both servers.
fetch_feed() uses the Simplepie class to fetch and manipulate the RSS – in constrast to the functions it replaces, wp_rss(), get_rss() etc., which stuck with good old Magpie and Snoopy to get the job done.
And the use of the function itself is simplicity:
if(function_exists('fetch_feed')){
$uri = 'http://themocracy.com/feed/';
$feed = fetch_feed($uri);
}
Notice the function_exists check, to ensure compatibility…
The simplepie object returned is complex (some might say a touch clumsy, but probably necessarily so in order to handle the wide variety of XML lumped together under the heading RSS). Don’t try to work with it directly…
Instead, what Simplepie has is methods to get at the data:
This code retrieves the overall feed title and then loops through the RSS items, grabbing and outputting the relevant item data.
printf( $feed->get_title());
foreach ($feed->get_items() as $item){
printf('<p><a href="%s">%s</a></p>',$item->get_permalink(), $item->get_title());
printf('<p>%s</p>',$item->get_description());
printf('<p><small>%s</small></p>',$item->get_date('j F Y | g:i a'));
}
That’s the basic idea – see the Simplepie docs for more…
You might want to try it as a quick way of getting a Twitter feed onto a page – or any other ideas for using RSS feeds directly…?




Excellent tutorial, there is very little info about the new fetch_feed function, so this & SimplePie were great places to start, thanks!
P.S. Here’s a direct link to the list of simplepie functions: http://simplepie.org/wiki/reference/start
Wow thanks dude, it is completed my learning today. On the way to simple pie.
thank you
now, I can use it in my blog…
Hiya — so I’ve used this feature in a client’s site here for the FB updates:
http://greenmommyblog.com/
However, the problem is that it can sometimes take an hour or even longer for an update to appear. What do you think the problem might be here?
Lara