Manipulating the RSS feed content with Custom Fields

Did you ever want to add a specific content just for your RSS readers in a specific post? Well this can also be done with custom fields. 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 just create a custom field called “coolcustom” and add any value you like. You can add google adsense in there, or any other ads/images/text that you like. In the second technique, we will show you how to add additional text to post titles in RSS. This comes very handy when you are running a blog and have guest posts / sponsored posts. By utilizing this technique, you can tell your RSS readers in the title whether a specific post is a sponsored post or a guest post. For example if your title was “Start Microblogging in WordPress with Wumblr” and it was a sponsored post, then you can change it to “Sponsored Post: Start Microblogging in WordPress with Wumblr“. Same if someone wrote a guest post etc. To accomplish this, open your theme’s 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');

The code above search for two custom fields name “guest_post” or “sponsored_post”. If any of these two custom fields are found with a value “true”, then it will add the appropriate text before the title. This technique can be utilized in various ways to fit whatever you like.

Rate this post