Quick n’ Dirty Feed FootersFiled Under: Snippets, plugins

This is the fourth of my Quick N Dirty plugin posts. In the first I used the the_content filter to repeat the links used in a post, at the end of that post. In this post I am going to the_content again, but this time I am specifically using it in place of another filter that could get you quite confused.
It is a pretty common thing to do these days, add a footer to each post in your feed. There are a few plugins that not only let you do it but give you a handy admin page to alter the content as well. Nothing beats knowing how to do it yourself though.
If you are going to do it yourself, there is one area that provides some confusion. There is a filter that sounds like it should be used, but simply won’t work for you. That filter is the_content_rss.
The problem is that the_content_rss is used in rss, and rdf feeds, but importantly not in the rss2 feeds which is what we all tend to use these days. So if you want to change the content of your feeds, for the moment at least, you need to use the_content and check to see whether a feed is being requested. Luckily this is very easy.
Here’s the plugin:
<?php /* Plugin Name: Quick n' Dirty Feed Footers Plugin URI: http://www.wp-fun.co.uk/2008/01/27/quick-n-dirty-feed-footers/ Description: Adds content to the bottom of each rss2 post Author: Andrew Rickmann Version: 1 Author URI: http://www.wp-fun.co.uk */ //This is the line that adds your filter into the list. // 'the_content' is the name of the filter // 'qnd_feed_content' is the name of the function, below add_filter( 'the_content' , 'qnd_feed_content' ); function qnd_feed_content( $content ){ //make sure that a feed is being requested if ( is_feed() ) { //create a string containing the html we want to add to the bottom $additional_content = '<div style="border:1px solid #ccc; padding:5px; background-color:#DBD2DC">'; $additional_content .= '<h2 style="margin:0 0 5px 0; font-size:95%; color:#AC26A7">Interesting Message</h2>'; $additional_content .= '<p>The content of the interesting message</p>'; $additional_content .= '</div>'; //return the content, and the additional content to WordPress return $content . $additional_content; } else { //if we are not within a feed just send back the original content return $content; } } ?>
I have used a basic WordPress tag - is_feed() - to check if a feed is being accessed. Once known the content can be modified. Very important, as with previous plugins is to return the content without changes if it is not a feed. As this is a filter, if no content is returned then no content will be displayed on screen.
Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.
- Permalink
- Andrew Rickmann
- 27 Jan 2008 3:14 PM
- Comments (0)