13

I want to show the whole content of every page in feed .I search for it and found some plugin but I could not solve my problem.

I want when I enter http://swissaudio.com/craftsmanship/feed it provides me the page content in feed. How can I do that?

  • just to be sure, are you asking whether to show the entire content of your posts in the feed rather than just a summary, or to add all pages (post_type of page) to the feed in full as well? – majick May 19 '16 at 10:19
  • @majick I want to show all content of page in feed .I use this code also but result change occur add_filter('request', 'feed_request'); function feed_request($qv){ $rss_post_types = array('post', 'page'); if(isset($qv['feed']) && !isset($qv['post_type'])) $qv['post_type'] = $rss_post_types; return $qv; } – raxa May 19 '16 at 10:24
  • at first glance it looks like that should work fine, make sure you have disabled any of the plugins you were testing and also disable any caches while testing. – majick May 19 '16 at 10:34
  • @majick due to above code feed work for home page .please check this site http://swissaudio.com/feed/ I want to show the content of each page in feed i.e for craftsmanship page I would like to do something like this http://swissaudio.com/craftsmanship/feed/ but it shows no result – raxa May 19 '16 at 10:49
  • 3
    this has proven to be very tricky I have tested a few more things with no luck so opened a bounty for this as I'd like to know the solution myself. :-) – majick May 21 '16 at 9:17
7
+50

First set the post type to display on main feed page i.e. /feed using pre_get_posts hook

$q->set('post_type', array('post', 'page'));

On individual page WordPress shows comment feed then set it to false and display page content in feed.

$q->is_comment_feed = false;

In feed template WordPress calls the_excerpt_rss() which calls get_the_excerpt() so using excerpt_length filter change the length to max.

Complete Example:-

add_action('pre_get_posts', 'wpse_227136_feed_content');
/**
 * Set post type in feed content and remove comment feed
 * @param type $q WP Query
 */
function wpse_227136_feed_content($q) {
    //Check if it main query and for feed
    if ($q->is_main_query() && $q->is_feed()) {
        //Set the post types which you want default is post
        $q->set('post_type', array('post', 'page'));
    }

    //Check if it feed request and for single page 
    if ($q->is_main_query() && $q->is_feed() && $q->is_page()) {
        //Set the comment feed to false
        $q->is_comment_feed = false;
    }
}

add_filter( 'excerpt_length', 'wpse_227136_excerpt_length', 999 );
/**
 * Filter the except length to full content.
 *
 * @param int $length Excerpt length.
 * @return int $length modified excerpt length.
 */
function wpse_227136_excerpt_length( $length ) {
    if (is_feed() && !get_option('rss_use_excerpt')) {
        return PHP_INT_MAX;
    }

    return $length;
}
  • ahh the comments default to true. so that's it..! well,it would seem to make sense if doing this that /page/comments/feed/ would actually show the comments instead, any idea how to do that? – majick May 21 '16 at 16:54
  • Well I think for this we need to add new rewrite rules. Because WP doesn't support it by default, Instead it support query string withoutcomments=1 as answered by cjbj. I am not good at all in URL rewriting :D – Sumit May 21 '16 at 17:12
  • 1
    not good with URL rewriting either, decided to turn it around and add support for withcomments=1 in my answer. – majick May 22 '16 at 11:20
  • for the excerpt length filter, should it not check get_option('rss_use_excerpt') not !get_option('rss_use_excerpt) because if it is already false an excerpt length filter would not be needed, it would display the full content already. O_o – majick May 23 '16 at 13:58
  • Well I am not sure why but if you check this line core.trac.wordpress.org/browser/tags/4.5/src/wp-includes/… in template it doesn't effect this setting that's why I am just correcting it that what it does suppose to do. – Sumit May 23 '16 at 14:33
4

This may not be ideal, but it is a beginning. First make sure that the full content is in the feed:

function fullcontentfeed($content) {
    global $post;
    $content = $post->post_content;
    return $content;
    }
add_filter('the_excerpt_rss', 'fullcontentfeed');

You should then see the full feed at this url

http://swissaudio.com/craftsmanship/feed/?withoutcomments=1

You can then use add_rewrite_rule to redirect visitors from /feed/. Far from ideal, but maybe a start for somebody else to work on.

  • using return get_the_content_feed() would be better because it applies the_content filters also... but unfortunately this outputs the full content in the feed's description field instead on the content field. – majick May 21 '16 at 17:06
3

As mentioned by @Sumit, you need to turn off the comments feed for a page (which I find really strange since by default comments are off on pages?) ... this is what I ended up with (allowing for getting the page comments feed with ?withcomments=1 if wanted):

add_action('pre_get_posts', 'rss_page_feed_full_content');

function rss_page_feed_full_content($q) {
    // Check if it feed request and for single page
    if ($q->is_main_query() && $q->is_feed() && $q->is_page()) {
        //Set the comment feed to false
        $q->set('post_type', array('page'));
        // allow for page comments feed via ?withcomments=1
        if ( (isset($_GET['withcomments'])) && ($_GET['withcomments'] == '1') ) {return;}
        $q->is_comment_feed = false;
    }
}

But for displaying the page content, since the feed template actually checks rss_use_excerpt to decide whether to display full text or summary (set on Settings -> Reading page) then this needs to be overridden if you want the full content to display for a page feed (so that you can have the main option set to whatever you like for posts.) Otherwise whatever else you do the content may end up in the description field of the feed instead of the content field.

add_filter('pre_option_rss_use_excerpt', 'page_rss_excerpt_option');

function page_rss_excerpt_option($option) {
    // force full content output for pages
    if (is_page()) {return '0';}
    return $option;
}

And finally, to get the RSS description field to display a page excerpt, you might have to do this (which is basically a copy of wp_trim_excerpt without strip_shortcodes) - well, I did anyway but it might be due to some weird shortcode behaviour on the page I was testing:

add_filter('the_excerpt_rss','rss_page_excerpt');

function rss_page_excerpt($excerpt) {
    if (is_page()) {
        global $post; $text = $post->post_content;
        // removed this line otherwise got blank
        // $text = strip_shortcodes( $text );
        $text = apply_filters( 'the_content', $text );
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters( 'excerpt_length', 55 );
        $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
        $excerpt = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return $excerpt;
}
  • Oh I am really sorry :D I have lost my mind I was reading it withoutcomments now I SEE it is withcomment :D LOL deleting my comments ;) – Sumit May 23 '16 at 15:42
  • no probs this one bent my mind out of shape for sure. – majick May 23 '16 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.