Wordpress - How to show page content in feed?
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;
}
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.
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;
}