WordPress: Modify page RSS feed to display full content

One of our clients has another developer building a mobile app for their website which is dependent on pulling content from their WordPress driven site via RSS Feeds.

Obviously for the news posts and so on there was no need to modify anything, but they also wanted to pull an RSS feed of their static page content, and by default the /feed attached to every page in WordPress loads the RSS Comments feed template.

How to modify page feed to display full content

I actually couldn’t find any one that had posted a solution to this issue so I went hunting through the WordPress core to find out what was causing the pages to load the comments-feed template instead of the normal RSS2 template.

The best I could track down is that pages, by default, only load the comment feed (although I couldn’t find exactly where this was defined). In wp-includes/functions.php:1183 we can see the function that is handling┬áthis:

    function do_feed_rss2( $for_comments ) {
	if ( $for_comments )
		load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
	else
		load_template( ABSPATH . WPINC . '/feed-rss2.php' );
    }

So what we need to do is change this behaviour a little bit (whilst obviously not modifying the core!).

Open your own theme’s functions.php file and add the following code:

	/**
	 * Function to stop 'pages' from using
	 * the default 'comment feed' layout
	 *
	 */
	function pa_force_page_feed_to_load_full_content($for_comments)
	{
			if(is_page()):
				load_template(ABSPATH . WPINC . '/feed-rss2.php');
			elseif ($for_comments):
				load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
			else:
				load_template( ABSPATH . WPINC . '/feed-rss2.php' );
			endif;
	}
	remove_action( 'do_feed_rss2', 'do_feed_rss2', 10, 1 );
	add_action( 'do_feed_rss2', 'pa_force_page_feed_to_load_full_content', 10, 1 );

Essentially what we’re doing here is rewriting the original do_feed_rss2 function and modifying it with an extra conditional. So if the query is a ‘page’ then load the feed-rss2.php template, otherwise if the feed type requires a comments feed (eg any post on your site automatically has an RSS feed available for the comments), then display the comments feed, and then fallback to the normal feed-rss2.php template.

So we’ve hijacked the function and all that’s left to do is remove the original action and replace it with our customised action.

And that’s it – now your WordPress ‘pages’ will display a full-content feed instead of a comments feed.

Comments