How to: Insert ads on your RSS feed

Did you ever wonder how some people are able to display ads on their rss feed? Sure, you can modify core files to do it, but that’s not a good idea at all. Here’s a clean way to insert ads (or anything) on your rss feed, without having to hack any core file.

In order to achieve this recipe, paste the code below to the functions.php file from your theme. If your theme doesn't have a functions.php file, create it.

<?php
function insertAds($content) {
    $content = $content.'<hr /><a href="http://www.wprecipes.com">Have you visited WpRecipes today?</a><hr />';
    return $content;
}
add_filter('the_excerpt_rss', 'insertAds');
add_filter('the_content_rss', 'insertAds');
?>

Here, we first create a function called insertAds(), which concatenate a code containing our advertisment to the $content variable, which contains the content of the post.
Then, we use the add_filter() function to overwrite the the_content_rss() function with our insertAds() function. We use another filter to overwrite the_excerpt_rss() function as well.
That's all, your rss feeds now displays your ads!

Credits goes to k-ny for this awesome recipe!

Related Posts

No related posts.

53 Responses

Nov 13 2008 10:00

Héhé, merci pour le lien ;) Du coup je m’abonne !

Nov 13 2008 11:44

I don’t think this works for Atom feeds however. I’ve tried to modify feeds by hooking the_content_rss and not had my functions executed. If you look in feed-atom.php, it calls the_content, not the_content_rss. feed-rss.php appears to correctly use the_content_rss.

Nov 13 2008 17:47

@Julien: Merci à toi pour cet excellent article!

@King Rat: I didn’t tested it with Atom feeds, only rss. I’ll take a look :)

Nov 13 2008 18:59

Has anyone tried this? Is there any working demo?
Thanks in advance!

Nov 14 2008 02:22

Thank you for the great recipe! Tried to look for this a long time.

I tried this, and actually it works.
But I get this error message:

Warning: Cannot modify header information – headers already sent by (output started at /home/edwinp/public_html/wp-content/themes/flashnews/functions.php:548) in /home/edwinp/public_html/wp-includes/feed-rss2.php on line 8

Any suggestion how to fix this?

Nov 14 2008 05:01

I think feedburner can easily put your adsense ads in your feeds…. this is perfect to those who do not use feedburner

Nov 14 2008 08:37

@Edo: Weird…I didn’t had the problem so I can’t help you, sadly :(

@Popular Technology: Yes, feedburner can put adsense in your feed, but this recipe should help if you want to include other ads as such as affiliate marketing ads, for exemple.

Nov 14 2008 09:29

@Popular Technology

You can also use this trick to insert a copyright. :)

@Edo

It is an encoding problem. We must save the functions.php in utf-8 without BOM (sometimes done to save through the theme editor repair the problem)

Ps: Sorry for the automatic translation

Nov 14 2008 23:57

Is this better than using a plugin for putting a text of line in the rss-feed?

Nov 15 2008 00:04

@Kaj Rietberg: It is basically the same effect. It mostly depends if YOU prefer using a plugin, or play with code :)

Nov 15 2008 15:48

Thanks Man

but still ads is not showing up !!

Nov 17 2008 03:25

What is the plugin that do similar effect that would you recommend? I tried to save the function.php in utf-8, still doesn’t work :(

Dec 04 2008 10:00

not working for me… :(

input this to my functions.php
function insertAds($content) {
$content = $content.’ads link’;
return $content;
}
add_filter(‘the_excerpt_rss’, ‘insertAds’);
add_filter(‘the_content_rss’, ‘insertAds’);

Dec 04 2008 11:25

@muhammad hakim: Do it works for you if you use the sample code I have provided?

Dec 04 2008 21:55

@muhammad hakim

Seems that the filter don’t work on WP 2.5. There’s a bug in WordPress core. To avoid the problem, try something like this:
function gopostrss($content) {
if(is_feed()){
$content = ‘text before content’.$content.’ and after the content !’;
}
return $content;
}
add_filter(‘the_content’, ‘gopostrss’);

Ps: I added this hack on the original source ;)

Dec 15 2008 10:53

I tried the code in the post, and it didn’t work. Then I tried julien’s code, and it worked! Thank you julien!

Dec 15 2008 11:00

@Betty: Yes, seems that there’s a bug in WP 2.5 and the code didn’t works. Glad Julien’s help was useful to you!
(Nice gravatar, btw ;) )

Dec 16 2008 06:01

@Jean-Baptiste Jung:
Perhaps not only a bug in WP 2.5, because I’m using WP 2.7 now. :)
You gravatar is very cute, too. ;)
BTW: I wrote a post about it and sent a trackback to here, but the trackback is not appearing…

Jan 05 2009 05:38

I think this is a great script. It is work for WP 2.7 ?

Jan 05 2009 15:03

@Busby SEO: Yes, it does :)

Jan 06 2009 16:05

Hi Jean,
Thanks for your confirm.

Jan 10 2009 10:07

Hi,

that looks slick. Can I add picture rather then a text link as an add.

Any hints how the code has to look like ?

I use wp125-ad-plugin-wordpress to dispaly my ads in the blog and WP 2.7

Thank you in advance

icewalker

Jan 10 2009 10:19

@icewalker: Sure you can, just use the <img tag.

Mar 03 2009 02:54

thanks for this tip, how do I get ads to show up in my comments RSS feed?

thanks

Mar 31 2009 08:59

can’t get it to work at http://www.marcoraaphorst.nl/feed/ maybe because of Feedburner?

Jun 21 2009 21:47

thanks to share this info. i never think about this before ( we can add ads on RSS feed)

Aug 23 2009 08:37

great, i’ll use your trick to add my adsense ads on my RSS.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required