Family Encyclopedia >> Electronics

How to add content and fully manipulate WordPress RSS feeds

A while back we shared how you can control your WordPress RSS footer with the 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 that is displayed in the footer of each post. What if you want to display different text for each post in your RSS feed? 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 are bringing you this article that will show you how to add any type of content to your WordPress RSS feeds. This hack will put you in control of your WordPress RSS feeds and you can manipulate it in any way you want.

Note:This trick is not for beginners. Only users who are comfortable with editing the functions.php file and have some knowledge of php should attempt this. Beginner users should use the plugin mentioned in the previous article 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 hack will allow you to display different text, ads, images, 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."

". $ coolcustom". "; else $ content = $ content; devolver $ content; add_filter ('the_excerpt_rss', 'wpbeginner_postrss'); add_filter ('the_content', 'wpbeginner_postrss');

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

Some of you will say, but they just did what the RSS footer plugin does with a function. Yes and no. Yes, we are adding the code in the post footer, but this text is not the exact same text for each post. It is different because you specify a different text for each post through custom fields. This trick will be very useful to comply with the new FTC guidelines for blogs that have all types 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, you'll find it very useful. Some bloggers have a custom style to display each different type of post, so their users can tell them apart. But by the time these posts go into a reader, all the styles are gone. That's when this trick comes in handy. In this trick we will add any text 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 achieve this, open your functions.php file and add the following code 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, 'sponsor_post', verdadero); if ($ gpost! == ") $ content = 'Guest Post:'. $ content; elseif ($ spost! ==") $ content = 'Sponsored Post:'. $ content; else $ content = $ content; devolver $ contenido; add_filter ('the_title_rss', 'wpbeginner_titlerss'); 

Code explanation:

We are using the function called wpbeginner_titlerss which runs a global wp_query to search each post if it contains $gpost or $spost. These two elements basically look for two specific custom fields called "guest_post" or "spons_post". If someone has added these custom fields with a true value, then the code will add it to the text. If not, you will only see the normal title. You can first see the code you are looking for if $gpost is true, if that is not true if $spost is true. If that is not also defined, then it displays the normal content. But if any of them were true, then it displays the different text you specify here. We are using $content string to display the post title.

Now that was only displaying custom fields in the title. Do you want to display the category name in each title? Well then you just need to paste the following code into your functions.php file:

 function wpbeginner_cattitlerss ($ content) $ postcat = ""; foreach ((get_the_category ()) como $ cat) $ postcat. = '('. $ cat-> cat_name. ')'; $ content = $ content. $ postcat; devolver $ contenido; add_filter ('the_title_rss', 'wpbeginner_cattitlerss'); 

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

3. Add the same text in all RSS posts

If you just want to add the same text, then you can also use the plugin called RSS Footer by Joost because it's 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 = 'Esta publicación fue escrita por Syed Balkhi'. $ content.'Check out WPBeginner '; devolver $ contenido; add_filter ('the_excerpt_rss', 'wpbeginner_postrss'); add_filter ('the_content', 'wpbeginner_postrss'); 

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

This would be very useful if you wanted to sell ads in specific RSS feeds, add custom FTC guidelines, or just wanted more control over your RSS feeds.

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