How to Customize WordPress RSS Feeds

How to Customize WordPress RSS Feeds

If you have a WordPress website. You indeed publish new articles or post daily or weekly for your users. Then you have to present them in front of your users. For this, you have a good option of RSS feed.

RSS feed is designed to show recent content and deliver it to the users. It allows users and applications to get regular updates from a blog or website they like. It is an XML text file containing the site’s updated information, which gets plugged into a feed reader that users can subscribe to.

Moreover, the RSS feed allows users to track their favorite websites without visiting each site manually. The WordPress RSS feeds shows your recent post content by default, but you can not customize that content for your RSS feed users.

Here, we will explain How to Customize WordPress RSS Feeds. And how to add content easily and control your WordPress RSS feeds.

Adding Custom Content to WordPress RSS Feeds 

You can add custom content to your WordPress RSS feed by using a plugin. It is the easiest and simplest way to add custom content to the WordPress RSS feed.

The All in One SEO Plugin

It is the best WordPress SEO plugin that easily optimizes your website SEO.

Now, install and activate the All in One SEO for WordPress plugin.

Go to plugins >> add new >> All in One SEO for WordPress plugin >> install >> activate.

After activation, set up All in One SEO for WordPress.

After that, go to All in One SEO » General Settings page >> switch to the RSS Content tab.

You can add content you want to display before and after each RSS feed. You can also use tags to add links and other metadata to the custom content.

After the changes, click the save button. All in One SEO will add custom content to every RSS feed.

Add Content to WordPress RSS Feed using Code.

The above-stated method is the easiest way to add custom content to WordPress. However, it adds the content to all articles in your WordPress feed.

There are times you want to add content to specific posts. You can do this with some simple steps that will help you add content to your RSS feed using custom code snippets.

You can add the code to your website using the custom Code Snippets plugin through a functions.php file.

Let us see some examples of manually adding custom content in WordPress RSS feeds.

1. Add Custom Field Data to WordPress RSS Feeds

The Custom fields option in WordPress allows adding extra metadata to WordPress posts. But, this metadata does not come under RSS feeds by default.

You can use the given code to retrieve and show custom field data in your WordPress RSS feed.

 function wpb_rsstutorial_customfield($content) {

global $wp_query;

$postid = $wp_query->post->ID;

$custom_metadata = get_post_meta($postid, 'my_custom_field', true);

if(is_feed()) {

if($custom_metadata !== '') {

// Display custom field data below content

$content = $content."<br /><br /><div>".$custom_metadata."</div>

";

}

else {

$content = $content;

}

}

return $content;

}

add_filter('the_excerpt_rss', 'wpb_rsstutorial_customfield');

add_filter('the_content', 'wpb_rsstutorial_customfield');

The above code checks if the custom field has data inside and displays the RSS feed. After that, it simply adds custom field data below the content.

2. Additional Text to Post Titles in RSS feed

If you have to show additional text to some post titles in the RSS feed, Below is how to add custom content to post titles.

Example 1: Adding up Data from Custom Fields to Post Title in RSS Feed 

First, save the content you want to display as a custom field. Then, you can add the following code to your website.

 function wpb_rsstutorial_addtitle($content) {

global $wp_query;

$postid = $wp_query->post->ID;

$gpost = get_post_meta($postid, 'guest_post', true);

$spost = get_post_meta($postid, 'sponsored_post', true);

  

if($gpost !== '') {

$content = 'Guest Post: '.$content;

}

elseif ($spost !== ''){

$content = 'Sponsored Post: '.$content;

}

else {

$content = $content;

}

return $content;

}

add_filter('the_title_rss', 'wpb_rsstutorial_addtitle');

The code examines the custom fields. If they are not empty, it appends the value of the custom field to the post title in your RSS feed.

Example 2: Category Name to Post Title in RSS Feed

For this example, If you want to show category name in post’s title.

Add the following code to your website:

 function wpb_rsstutorial_titlecat($content) {

$postcat = "";

foreach((get_the_category()) as $cat) {

$postcat .= ' ('.$cat->cat_name . ')';

}

$content = $content.$postcat;

return $content;

}

add_filter('the_title_rss', 'wpb_rsstutorial_titlecat');

 The above code will display categories with post titles in RSS feeds.

3. Add Custom Content to Posts with Categories or tags.

Now, If you have to add the custom content only for posts under particular categories or tags.

you can use the following code

 function wpb_rsstutorial_taxonomies($content) {

 

if( is_feed() ){

 

// Check for posts filed under these categories

if ( has_term( array( 'travel', 'news' ), 'category' ) ) {

 

$content = $content."<br /><br />For special offers please visit our website"; 

 

}

}

return $content;

}

add_filter('the_excerpt_rss', 'wpb_rsstutorial_taxonomies');

add_filter('the_content', 'wpb_rsstutorial_taxonomies');

If you want to modify this, you can change the code to target tags. An example of targeting specific tags is below:

function wpb_rsstutorial_taxonomies($content) {

 

if( is_feed() ){

 

// Check for posts filed under these categories

if ( has_term( array( 'holidays', 'blackfriday' ), 'post_tag' ) ) {

 

$content = $content."<br /><br />For special offers please visit our website"; 

 

}

}

return $content;

}

add_filter('the_excerpt_rss', 'wpb_rsstutorial_taxonomies');

add_filter('the_content', 'wpb_rsstutorial_taxonomies');

4. Adding Featured Image

 WordPress RSS feed does not display featured images for your post by default. You can add it manually to your RSS feed.

 function wpb_rsstutorial_featuredimage($content) {

global $post;

if(has_post_thumbnail($post->ID)) {

$content = '<p>' . get_the_post_thumbnail($post->ID) .

'</p>' . get_the_content();

}

return $content;

}

add_filter('the_excerpt_rss', 'wpb_rsstutorial_featuredimage');

add_filter('the_content_feed', 'wpb_rsstutorial_featuredimage');

The code checks if a post has a featured image and shows it with the rest of your post content.

To wrap up, In this article, you have learned  How to Customize WordPress RSS Feeds by different methods.

 

Leave a Comment