RSS Tricks: How to Insert Ads (or more) Into Your RSS Feed

Welcome to our “RSS Tricks” series of tutorials! We’re going to be posting a new RSS-related tutorial every week for the next 8 weeks, so here it goes.

In this tutorial we’ll be showing you how to insert ads, or anything else for that matter, into your RSS feed.

This method will enable you to insert any adverts or other content into your RSS feeds without having to touch any core files. It can be a good way to promote certain products or services, but use it wisely. Adding too many ads (or any other unrelated content) could put off subscribers.

The Fix

All it takes to insert new content into your RSS feed is to add a few lines of code to your theme’s functions.php file. If your theme doesn’t have one, you can create it yourself.

So once you’ve opened the functions.php file, just add in the following code:

How It Works

Let’s go through this bit by bit.

First we created a new function called insertAds() , inside which we concatenated the HTML of the actual advert we want to include in the RSS feed to the $content variable (which contains the original content of the post).

Next we just use the add_filter() function to add our newly created insertAds() handler to the the_content_rss filter . The same is done for the the_excerpt_rss filter too.

Hmm, good idea but the problem is this.
When I insert an ad with this method the ad gets appended to every post on the rss feed. Which is a bit annoying for the subscriber.
I thought it would be like, every 3rd or 4th item in the feed would be an ad. I wonder if there’s a way to do this?