Quick Tip: Adding New Feeds to WordPress

VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Sample XML Code

Adding a new type of feed to WordPress is actually fairly simple. The main function you need to use is `add_feed()`. The `add_feed()` function in itself is also extremely simple. It accepts two arguments: the “name” of the feed (the slug that should be used in the URL) and the callback function that outputs the feed.

There are two major things to note when adding a new feed to WordPress:

  1. You need to flush the rewrite rules before the new feed will work. This is not something you want to do constantly, so you’ll need to identify a way to only flush the rules when you’ve changed or added a new feed. The codex recommends doing this on plugin activation, but that can be problematic for mu-plugins or plugins that are network-activated on a multisite network. Instead, unless someone has a better recommendation, I usually add a “version” number to the database (either using `update_option()`\`get_option()` or, in the case of a network-activated plugin, `update_site_option()`\`get_site_option()`) and retrieve that before deciding whether the rules need to be flushed.
  2. You need to make sure you visit the new feed at /feed/[your-feed-name] rather than just /[your-feed-name]. After much debugging, I discovered that, while the feed appears to load just fine at /[your-feed-name], WordPress actually sends a 404 status header; if you visit the feed at /feed/[your-feed-name], it sends the 200 status header.

If you’re interested in taking a look at a sample plugin that adds a new type of feed to WordPress, in this case, a JSON feed of all sites registered in a multisite network, you can visit my Github repository for the site-list-feed plugin.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Leave a Reply