In Brief: Adjusting the WordPress RSS Feeds

I suppose that I shouldn’t have been surprised that a site’s RSS feed production fell under WordPress core functionality. After all, it is fairly powerful, producing RSS, RSS2, Atom, and RDF presentations of all posts as well as posts by category and tag and author and … and it does fall outside what the user sees (so outside theme development), … Still, the options for RSS control are extremely limited: Would you like to offer full post content or post excerpts? That’s a bit like asking a kid if he’d like his broccoli raw or steamed. It’s not a configuration option to disable RSS, nor is it an option to offer no content and no excerpt — just the notice indicating new activity and the URL to find it.

Of course, if you’re willing to look under the hood and you’ve got your PHP crowbar with you, you can get it done.

Solution Sketch

  1. Create a child theme.
  2. Copy and modify the original feed-{type}.php files from {WORDPRESS}/wp-includes/ to your {WORDPRESS}/wp-content/themes/{CHILD-THEME}/ directory.
  3. Add a few lines of code to the child’s functions.php file to say you’ll be using your new & improved RSS templating functions instead.

Rationale?

WordPress core (where the RSS production code lives) and WordPress themes (where functions.php, the traditional place to make basic runtime modifications outside of the plugin framework, lives) are subject to change with updates by complete overwrite. You modify things there at your own risk.

The accepted way to make sure your changes persist is to use a “child theme.” A child theme references the parent and keeps everything about the parent except for what is explicitly modified. In the case of the functions.php file, the parent’s is loaded and then the child’s. That’s a little different than wholesale replacement of the file; we have to write a block to undo the parts in the parent’s code we don’t like and then continue as we’d prefer.

In my case, I added the following to the child’s functions.php file:

## RSS2
function my_custom_rss() {
       load_template( TEMPLATEPATH . '/../twentytwentyone-child/my-feed-rss2.php');
       get_template_part( 'feed', 'rss2' );
}
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'my_custom_rss', 10, 1 );

## ATOM
function my_custom_atom() {
       load_template( TEMPLATEPATH . '/../twentytwentyone-child/my-feed-atom.php');
       get_template_part( 'feed', 'atom' );
}
remove_all_actions( 'do_feed_atom' );
add_action( 'do_feed_atom', 'my_custom_atom', 10, 1 );

You may gather that this blog uses the WordPress twentytwentyone theme with a few customizations. The code above replaces the stock RSS2 and Atom feeds. The method is pretty PHP & WordPress standard: declare a function and drop it into a queue to be executed when an action is triggered. Here, the action is do_feed_{type}. We throw away whatever the parent has put in the queue — hopefully it’s just the one! — and we replace it with our modified file inside the child directory, TEMPLATEPATH/../CHILDTEMPLATEDIR/my-feed-{type}.php. Inside each of those, I’ve tinkered with what gets displayed.

To completely disable a feed-type, follow the same process, but instead of loading a custom template in your handler, you might try dropping this function into the queue instead — though I suspect it would make feed readers angry:

function my_disable_feed() {
 wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
}

Instead, I think it would be more polite to 404 all URLs containing “feed” from inside the reverse proxy — assuming you have access, of course — rather than offering a feed and providing garbage outside the schema. [I believe I’ve seen that WordPress sites also advertise their feeds inside the pages; if so, it’d be polite to remove those references too.]

References

The WordPress codex, of course, has ample information, though it’s a bit challenging if (like me) you’re not so familiar with their framework. Quick browse through the official page toward the cluster of links at the bottom, to wit:

  • “WordPress Feeds”, the WordPress top-level page. (link)
  • “Customizing Feeds”, at the bottom of that page. (link)

Good luck! 🙂

By Joe

Puzzle Wrestler & Mountain Herder. Math & Computer Nerd since the 80s. Longtime linux (current debian, ubuntu, raspian, centos, gentoo), currently fighting feebsd. Over-complicates networks for fun, occasionally secures them for profit. Develops own tools & services (cli, web services, and lately some android). Degrees in Math, Belts in Aikido. Zen, Motorcycle, Ham Radio, Homebrew (Ale, not Radio), Coffee & Tea, some Mandolin & Fiddle, MDA Advocacy (son with Duchenne), …

Leave a comment

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