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) {<br></br> // do stuff with content<br></br> return $contentToFilter;<br></br>}

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.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

22 Nov 2014

WordPress 4.1 Improvements for Theme Developers

WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all...
17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
16 Sep 2009

What’s new with the Elemental WordPress theme?

Elemental is the upcoming theme framework for Pro Theme Design. It’s been in development for absolutely ages, and the code is really showing it’s maturity, and I am really pleased with the possibilities it opens up.The focus when developing Elemental...