WordPress and SimplePie functionality

SimplePie functionality for your blog:
function fetch_feed_modified($url) {
 require_onceย  (ABSPATH . WPINC . '/class-feed.php');
 $feed = new SimplePie();
 $feed->set_feed_url($url);
 $feed->set_cache_class('WP_Feed_Cache');
 $feed->set_file_class('WP_SimplePie_File');
 $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200));
 $feed->init();
 $feed->handle_content_type();

 if ( $feed->error() )
 printf ('There was an error while connecting to Feed server, please, try again!');

 foreach ($feed->get_items() as $item){
  printf('<li><a href="%s">%s</a>',$item->get_permalink(), $item->get_title());
 printf(' <small>%s</small></li>',$item->get_date());
 }
}

function ms_smallpie_feed($uri) {
 if(function_exists('fetch_feed_modified')) {
 fetch_feed_modified($uri);
 }
}

Put this inside your functions.php file, call ms_smallpie_feed function from your theme.

So, where does it all begin: two weeks or so ago I was asked to take a look at MagpieRSS Simplified plugin. There were some problems with feeds – not working. Poor readers, nobody to feed them ๐Ÿ™‚

Anyway, the problem was not their code, connection to feed server was failing sometimes.

But, here comes the bummer:ย  Magpie functionality is deprecated: check fetch_rss WordPress codex page. WordPress documentation guys advice fetch_feed functionality instead (that’s SimplePie for you and me). And, at the time of the writing, there were no documentation how to apply it. I snooped around a bit inside WordPress core files, found relevant functions. The code at upper half of post lets you add SimplePie functionality to your theme. The code is self explaining.

Of course,ย  if you don’t want to change much, following function inside your functions.php file will be enough too


function ms_smallpie_feed($uri) {
 if(function_exists('fetch_feed')) {
 fetch_feed($uri);
 foreach ($feed->get_items() as $item){
 printf('<li><a href="%s">%s</a>',$item->get_permalink(), $item->get_title());
 printf(' <small>%s</small></li>',$item->get_date());
 }
 }
}

Just don’t forget to call it from theme. It goes like this:


ms_smallpie_feed(www.moskjis.com/feed);