Skip to content

An Intro to WordPress Filters: How to Add Custom Stuff to your RSS Feed

Recently I have added some custom content to the RSS feeds on 2 of my websites.

Here on Binary Moon I have added a notice that tells people if a specific post is in the Art Directed category, so they know to visit my site to see something styled a bit differently, and over on WPVote I added an automatically generated thumbnail to show a screenshot of the website that has been written about. Check out the WPVote feed to see what I mean.

Both of these things are an excellent way to engage people more with the content, and there is no limit to what you could do. For example you could use this very simple code to:

  • Link to your Twitter account and other social networking or social bookmarking accounts
  • Add a copyright notice to your feed
  • list related posts or other posts in the same category
  • Promote other websites you own
  • Sell advertising to people – particularly valuable if you have a large RSS subscriber list

The code to do this is really simple as well. It makes use of the filter system that is integrated into WordPress to hook into the content, make some changes, and then output it again with the new additions.

Using a filter is easy, you just have to add some simple code to the functions.php file found in your theme. The basic usage looks like this:

function contentModifier ($contentToFilter) {
// do stuff with content
return $contentToFilter;
}

add_filter('filterName', 'contentModifier ');

There are actually loads and loads of filters available, and there’s a list of all the WordPress filters on the Codex. Many theme frameworks also add their own custom filters such as Elemental has around 33 of them (listed on the Elemental theme documentation), which allows you to change the theme without touching the code.

To add custom stuff to my RSS feeds I made use of 2 filters, that both used the same modification function. The filters are called the_excerpt_rss and the_content_rss. When the filter is executed the data to be modified is passed to the function you specify. The content can then be altered however you wish and returned.

In this example I am going to link to my Twitter page at the bottom of the content:

function bm_rssContent($content) {
	$content = $content . '<p>Like my blog? Why don't you <a href="http://twitter.com/binarymoon/">follow me on Twitter</a></p>';
	return $content;
}
add_filter('the_excerpt_rss', 'bm_rssContent');
add_filter('the_content_rss', 'bm_rssContent');

So what would you use something like this for? I bet you have lots of idea that I haven’t considered.

16 thoughts on “An Intro to WordPress Filters: How to Add Custom Stuff to your RSS Feed Leave a comment

  1. I’m trying this method but it doesn’t work. I think old posts doesn’t affected. I don’t created any new post after adding this code snippet to my functions.php file.

    • If you use feedburner or some other similar service then it will take a little while for the feeds to update, but they will update eventually.

      • I’m looking through blogaddress.com/feed/ not Feedburner. Also i don’t using any plugin for redirecting feed to Feedburner.

        By the way i’m using Wp 2.9.1.

  2. Hi Ben, i know this is off topic
    but could you make like a digg button for wpvote?

    so i can add like a “digg my post” or “tweet my post”
    in my site. I’m sure that it will be awesome if you could do that.
    it will put more interaction to the user

    If there’s already a feature like this in wpvote,
    i look everywhere for it but couldn’t find it

    Dian

    • Hi Dian. This is on my todo list, but it’s a complex feature to develop and I am currently not entirely sure how to do it… but I do have some ideas 🙂

  3. Ben,

    This code will work with WordPress Feeds, but when exporting to Feedburner, it will fail. I was doing a similar thing on a client’s site and it was not working. Then I used Joost’s plugin for guidance to see what he was doing.

    You would actually have to add it to the content of the RSS feeds.

    Did a whole write up How to Manipulate Your WordPress RSS Feeds

  4. Hey, thanks for the tutorial. It worked perfectly. Had a quick question though – after I get the extra content to load into the RSS Feed (for example if I’m viewing it in Safari), when I actually embed the RSS Feed on a website, that extra info goes away. Do you have any idea why that would happen? It’s been about 4 days as well – and I’ve tried clearing my cache several times. Thanks!

  5. I’m late to this party but I plan to use this trick to auto generate the content of the post based on content from custom fields. Hopefully it works as envisioned. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *