Forge Code

Using a WordPress Plugin to Disable the "Category" Field in RSS Feeds

A lot of the work in developing Forge Code has been working out ways of modifying WordPress’s default behaviour without hacking core WordPress files. The main reason that it’s not wise to modify core WordPress files is that performing a WordPress upgrade will overwrite any modifications you have made. If you want to retain your code modifications during an upgrade, you have to keep track of every file that has been modded, which is tedious and error-prone.

Unfortunately, as of WordPress 2.7.1, RSS feeds are not covered by the theme/template system, and the templates reside in the WordPress core. Fortunately, there are a large number of “hooks” throughout WordPress codebase which allow you to override default behaviours. These hooks make up WordPress’s Plugin API. There are two types of hooks: “actions” and “filters”. An action is a hook that simply executes a function, and a filter is a hook that executes a function with a parameter (often, but not always, text) where the function is expected to return something. It’s like a pipeline - a filter takes something in, and it has to put something out. You can do things like return an empty string based on a conditional, run a text filter to strip HTML tags, or convert from numbers to plain English month names. To use either of these types of hooks in a WordPress plugin, you “register” them with WordPress using either the add_action() or add_filter() functions.

I don’t (as of this post, anyway) use categories on my blog posts, so I found it somewhat annoying that WordPress added a <category> tag to my RSS feed XML. This tag is used by most RSS readers (in my case, NetNewsWire) to show what “category” a post or article belongs to. I don’t like presenting redundant or unnecessary visual information, since it only distracts readers from the actual content of each post. My aim in developing this plugin was to work out how to get rid of the <category> tag using a 100% modular WordPress plugin (no core hacking!), so that my feed only retained relevant information when people read my blog in their RSS reader.

Developing the Plugin

In WordPress 2.7.1, the /wordpress/wp-includes/feed-rss2.php file is used as the template for RSS2 feeds, which is the only type of feed I provide on Forge Code. Digging around in the WordPress source revealed that the <category> tag is added to the RSS feed on line 41 of feed-rss2.php with the following code:

<?php the_category_rss() ?>

The source for this function can be found on line 359 of /wordpress/wp-includes/feed.php. The function simply echoes the return value of the function get_the_category_rss(), which can also be found in feed.php, on line 317. A quick scan through get_the_category_rss() reveals that there is a filter hook at the end of the function on line 346:

return apply_filters('the_category_rss', $the_list, $type);

By adding a filter with the hook name the_category_rss, we can control what is returned by get_the_category_rss(), and therefore control what is added when the_category_rss() is called. Since I don’t want any <category> tag at all, I simply want my the_category_rss filter to return an empty string: ''.

To actually implement this, we have to do two things. The first is to create a filter function, and the second is to register that filter function using add_filter().

function nf_disable_feed_category() {
  return '';
}

This is my “return empty string” filter function. The “nf” prefix is my initials, which are added to avoid any potential naming clashes (force of habit from the Objective-C world). Since this function exists in the global execution space of PHP, we have to have a globally unique function name. To register the function, we add the following:

if (function_exists('add_filter')) {
  add_filter('the_category_rss', 'nf_disable_feed_category', 1000);
}
else {
  echo "Disable Feed Category is operational!";
}

The if statement checks to see if execution is occurring within a valid WordPress context - if it’s not, we should print out a nice message instead of throwing a PHP error. This could happen if you try and directly load the plugin URL from a web browser. The first argument to add_filter is the hook name that we want our filter to be attached to. The second argument is the name of the callback function to execute when this filter is run. The third argument is a ‘priority’ number, which is used to decide execution order when there are multiple filters registered for a particular hook name. Lower priority numbers will be executed earlier (the default is 10).

Packaging and Installation

This is all wrapped up into simple PHP file named disable-feed-category.php, and should be placed into /wordpress/wp-content/plugins/. To enable the plugin, you need to go to the “Plugins” page of your WordPress Admin page. If you have put the plugin file in the right spot, “Disable Feed Category” should show up in “Inactive Plugins”. Click on “Activate”, and that’s it. Your RSS and Atom feeds should no longer contain any <category> tags, and your news reader/aggregator should no longer show you unnecessary or meaningless metadata!

Download

The plugin can be downloaded from the Disable Feed Category Plugin for WordPress page.

Comments