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


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”

function wp_feed_on_page($data){
if(!preg_match("//", $data)) {
//no remark : return the content
    return $data;
} else {

    global $post;
    $page_feedurl =  get_post_meta($post->ID, 'page_feedurl', true);

//if no url attached to the page : return the content 
    if(!$page_feedurl) return $data;

    $rssfeed = include_once(ABSPATH . WPINC . '/feed.php');

//get the feed with simplepie, fetch_feed is wp2.8+
    $rssf = fetch_feed($page_feedurl);

//get the number set for page_feeditems
    $page_feeditems =  get_post_meta($post->ID, 'page_feeditems', true);

//get the item count from the feed
    $num_items = $rssf->get_item_quantity(); 

//if feeditems is 0, pick the whole feed
//if feeditems is >0, pick all items up to that number
    if($page_feeditems==0) {
        $page_feeditems = $num_items;
    } else {
        if($num_items< $page_feeditems) $page_feeditems = $num_items;

//get the items
$rss_items = $rssf->get_items(0, $page_feeditems); 

//build an unsorted list
    $rss .= ''; 

//add the list to the end of the content
    $data .= $rss;

//return the content
    return $data;


//hook it into "the_content" 
if( function_exists('add_filter') ) {
		add_filter('the_content', 'wp_feed_on_page'); 

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 *