add rss output to a wordpress page

A quick snippet to add an rss-output to a page in wordpress 2.8.

I could not find simple routine to quickly list classifieds and other rss feeds on incidental pages, this one works fine for me :

1) in HTML-view add a remark in the page

  1.           <!–wpfeedonpage–>

2) add two keys to the page meta
page_feedurl : the full url
page_feeditems : max number of items

3) add something like this to functions.php and hook it into “the_content”

  1. function wp_feed_on_page($data){
  2.  
  3. if(!preg_match("/<!–wp-feed-on-page–>/", $data)) {
  4. //no remark : return the content
  5.     return $data;
  6. } else {
  7.  
  8.     global $post;
  9.     $page_feedurl =  get_post_meta($post->ID, 'page_feedurl', true);
  10.  
  11. //if no url attached to the page : return the content
  12.     if(!$page_feedurl) return $data;
  13.  
  14.     $rssfeed = include_once(ABSPATH . WPINC . '/feed.php');
  15.  
  16. //get the feed with simplepie, fetch_feed is wp2.8+
  17.     $rssf = fetch_feed($page_feedurl);
  18.  
  19. //get the number set for page_feeditems
  20.     $page_feeditems =  get_post_meta($post->ID, 'page_feeditems', true);
  21.  
  22. //get the item count from the feed
  23.     $num_items = $rssf->get_item_quantity();
  24.  
  25.  
  26. //if feeditems is 0, pick the whole feed
  27. //if feeditems is >0, pick all items up to that number
  28.     if($page_feeditems==0) {
  29.         $page_feeditems = $num_items;
  30.     } else {
  31.         if($num_items< $page_feeditems) $page_feeditems = $num_items;
  32.     }
  33.  
  34.  
  35. //get the items
  36. $rss_items = $rssf->get_items(0, $page_feeditems);
  37.  
  38. //build an unsorted list
  39.     $rss .= '<ul>';
  40.         if ($num_items == 0) $rss .= '<li>nothing to see</li>';
  41.         else
  42.          $item_count=0;
  43.   foreach ( $rss_items as $item ) {
  44.              if($item_count++>=$page_feeditems) break;
  45.                     $rss .= '<li>';
  46.                         $rss .= '<a href="'.$item->get_permalink().'
  47.                        title="Posted '.$item->get_date('j F Y | g:i a').'"
  48.                        rel="nofollow">'.$item->get_title().'</a>
  49.                `  <br />
  50.                            '.$item->get_content().'<br /><br /><br />
  51.                ';
  52.             $rss .= '</li>';
  53.  
  54.         }
  55.     $rss .= '</ul>';
  56.  
  57. //add the list to the end of the content
  58.     $data .= $rss;
  59.  
  60. //return the content
  61.     return $data;
  62. }
  63.  
  64. }
  65.  
  66. //hook it into "the_content"
  67. if( function_exists('add_filter') ) {
  68.   add_filter('the_content', 'wp_feed_on_page');
  69. }

that does the trick.

the fetch_feed function is SimplePie so it only works from wordpress 2.8 up, I heard SimplePie stopped developing recently which may force WordPress to drop the library if it is not fostered or forked, so I would not use these functions on clients sites unless you keep track of it.

Posted in juust.

Leave a Reply

Your email address will not be published. Required fields are marked *