Wordpress’s dynamically generated feeds is one of its strongest points. However, there is a glaring omission with the feed generation code – there is no way to generate a feed by tag. Through work, I have been in several situations where creating a feed by tag is a product requirement.
With a small core mod, you can change your Wordpress blog to provide feeds by tag. Note: modifying core files has some ramifications. Mainly, the next time you upgrade your Wordpress instance, you must also merge in and upgrade all your core modifications. It’s important to keep core mods simple and well documented, so you’ll remember to do it again months down the line.
To add an RSS feed by tag, simply add the following lines to the top of your /wp-includes/feed-rss.php file AFTER the php header() function call.
$tag = (urldecode($_GET['tag'])); if (!empty($tag)) { query_posts("tag=$tag"); }
Within the broader context of the file, the code will look like this:
<?php /** * RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed. * * @package WordPress */ header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true); $more = 1; /* CORE MOD - Added feed by tag */ $tag = (urldecode($_GET['tag'])); if (!empty($tag)) { query_posts("tag=$tag"); } ?> <?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
Now, you’ll be able to add links to RSS feeds by tags in these formats:
http://www.mysite.com/feed/?tag=politics
http://www.mysite.com/feed/rss/?tag=politics
Easy, right?
