Multiple versions of RSS feed? One with full content, one with the excerpt

My approach would be to:

  1. On the Reading Options page, set the For each article in a feed, show... option to Full Text so that, by default, feeds will include the entire post.
  2. Pass a custom URL parameter to each individual feed when you want the excerpt instead of the full text. e.g., http://example.com/author/username/feed?format=excerpt. Make sure you don’t use a reserved term for the parameter name.
  3. Hook into the feed creation process and modify the content based on whether or not the custom parameter is present in the current feed’s URL.
  4. If it is present, override the feed’s content to only display the excerpt instead of the full text.
  5. When you want the full content, just use the regular URL. e.g., http://example.com/author/username/feed.

Here’s some code that does that, using format as the parameter name. You can use any parameter name you want, as long as you update each part of the code that references it. Put the code inside a functionality plugin.

function truncateFeedContent( $content )
{
    if( get_query_var( 'format' ) == 'excerpt' )
    {
        $content = get_the_excerpt();
        $content = apply_filters( 'the_excerpt_rss', $content );
    }

    return $content;
}
add_filter( 'the_content_feed', 'truncateFeedContent' );

function addFormatQueryVar( $queryVars )
{
    $queryVars[] = 'format';

    return $queryVars;
}
add_filter( 'query_vars', 'addFormatQueryVar' );

To avoid a conflict with the Advanced Excerpt plugin, add this code to the functionality plugin as well. It will disable Advanced Excerpt’s functionality on feed URLs, but leave it in tact for all other pages.

function stopAdvancedExcerptOnFeeds()
{
    if( is_feed() )
        remove_filter( 'get_the_excerpt', array( 'AdvancedExcerpt', 'filter' ) );
}
add_action( 'pre_get_posts', 'stopAdvancedExcerptOnFeeds' );

Leave a Comment