...

How to Add Content and Completely Manipulate Your WordPress RSS Feeds

168

A while back we shared how you can control your WordPress RSS Footer with a use of a famous plugin called RSS Footer by Joost. While the plugin is great, but it is very limited. You can only add text to the footer, and it is always the same text displayed on each post’s footer. What if you want to display different text for each post in your RSS post? What if you want specific posts to have a different title in RSS Feeds? What if you want to display a specific custom field in your RSS Feeds? Well this is why we present you this article which will show you how to add any type of content in your WordPress RSS Feeds. This hack will put you in control of your WordPress RSS Feeds and you can manipulate it in anyway you want.

Note: This hack is not for beginners. Only users who feel comfortable with editing functions.php file and have some knowledge of php should try this. Beginner users should either use the plugin mentioned in the article above, or consult professionals like us to do it for you.

1. Add a Custom Field to your WordPress RSS Footer

In this first example, we will show you how you can use custom field to display specific text/object in your WordPress RSS Feed. This trick will allow you to show different text, advertisement, image, or anything else for each post. First open your functions.php and paste the following codes in the php tags:

  function wpbeginner_postrss($content) {  global $wp_query;  $postid = $wp_query->post->ID;  $coolcustom = get_post_meta($postid, 'coolcustom', true);  if(is_feed()) {  if($coolcustom !== '') {  $content = $content."<br /><br /><div>".$coolcustom."</div> ";  }  else {  $content = $content;  }  }  return $content;  }  add_filter('the_excerpt_rss', 'wpbeginner_postrss');  add_filter('the_content', 'wpbeginner_postrss');

Now most of you are wondering how this code is functioning. So here is an explanation. We create a function called wpbeginner_postrss which runs a global wp_query to search in each post if a custom field called “coolcustom” is defined. If Cool Custom is defined then it shows the value after the post content. If there is no custom field defined, then the function by default shows just the post content and nothing else. We use the variable $content to display content. We are using if(is_feed) function and adding the custom text or other content to the main post content itself which you can see by the second filter. But it will only be displayed in the RSS Feeds due to our user of if(is_feed) function. By doing it this way, we avoid all the compatibility issues.

Some of you will say but you just did what RSS footer plugin does with a function. Yes and NO. Yes we are adding the code at the footer of the post, but this text is not the same exact text for each post. It is different because you specify different text for each post via custom fields. This trick will be very handy to comply with the new FTC guidelines for blogs that have all different type of posts.

2. Adding Additional Text to Post Titles in RSS

Does your blog have guest posts, sponsored posts, and review posts? Well if you do then you will find this very useful. Some bloggers have custom styling to display each different type of post, so their users can distinguish between them. But when these posts go in a reader, all the stylings are gone. That is when this trick comes in handy. In this trick we will add any text either before or after the title.

For example if your title was “Commercial WordPress Theme – StudioPress” and it was a sponsored post, then you can change it to “Sponsored Post: Commercial WordPress Theme – StudioPress”. Same if someone wrote a guest post etc.

To accomplish this, open your functions.php file and add the following code in there:

      function wpbeginner_titlerss($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', 'wpbeginner_titlerss');

Explanation for the code:

We are using the function called wpbeginner_titlerss which runs a global wp_query to search in each post if it contains either $gpost or $spost. These two elements are basically looking for two specific custom fields called “guest_post” or “sponsored_post”. If anyone has these custom fields added with a value true, then the code will add it in the text. If not then you will just see the normal title. You can see first the code looks for if $gpost is true, if that is not true whether $spost is true. If that is not also not defined, then it displays normal content. But if either one of them was true, then it displays the different text you specify here. We are using $content string to display the post title.

Now that was just displaying custom fields in the title. Do you want to display Category name on each title? Well then you should simply paste the following code in your functions.php file:

  function wpbeginner_cattitlerss($content) {  $postcat = "";  foreach((get_the_category()) as $cat) {  $postcat .= ' ('.$cat->cat_name . ')';  }  $content = $content.$postcat;  return $content;  }  add_filter('the_title_rss', 'wpbeginner_cattitlerss');

Explanation: We are using the function wpbgeinner_cattitlerss to get the category ID for each post and then displaying that right next to the title. So if the title is “Get Contact Form 7” now it would be “Get Contact Form 7 [Plugins]”. You can see that there is no if then variable in this code. We use $content for the main title and $postcat variable to define the category name. You rearrange that if you like.

3. Add Same Text on all Posts in RSS

If you just want to add the same text then you might as well use the plugin called RSS Footer by Joost because it is easier. But if you want to do it yourself this is how you do it. Open your functions.php file add the following code:

  function wpbeginner_postrss($content) {  if(is_feed()){  $content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner';  }  return $content;  }  add_filter('the_excerpt_rss', 'wpbeginner_postrss');  add_filter('the_content', 'wpbeginner_postrss');

Explanation: We are calling a function wpbeginner_postrss to add in each post a content before the post saying This Post was written by Syed Balkhi and after the content Check out WPBeginner. But we add the function if(is_feed), so it will only be displayed in RSS Feeds.

This would be very helpful if you wanted to sell ads on specific posts in RSS, add custom FTC guidelines or just wanted to have more control over your RSS Feeds.

Source: We used Joost’s RSS Footer plugin for a lot of guidance in writing this tutorial. The title RSS hack part we got from a French tutorial site and we added our own variables and gave it the ability to be custom titles per custom field.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.