|

How to add a custom RSS feed in WordPress

WordPress automatically generates RSS feeds for your posts, categories, tags, comments, and more. You can find the default RSS feed URL for your website by adding /feed/ to the end of your domain name. For example, https://webberzone.com/feed/.

However, sometimes you may want to create a custom RSS feed that includes only specific content from your website. For example, you may want to create a custom RSS feed for a custom post type, a custom taxonomy, or a combination of different criteria.

In my Top 10 – Popular Posts WordPress plugin has a cool feature that allows the popular posts to be accessed via a custom feed. The slug/path of the feed can be configured in the plugin settings page.

In this post, we’ll learn how to create a custom feed in WordPress using some simple snippets of code. If you’d like to skip the tutorial and study the code, check out this repository.

Step 1: Create the custom RSS template file

Before you start coding, decide what content you want to include in your custom RSS feed. It could be a specific category of posts, custom post types, or even content from an external source. For this example we’ll include posts from a post type called “custom-post-type” – yes I know this name lacks imagination.

WordPress allows you to create custom template files for various parts of your site, including RSS feeds. To create a custom RSS feed, you’ll need to create a new PHP file in your theme or plugin directory. In my case, I coded this as a file within my plugin.

The easiest way to make this template is to copy the one that comes with WordPress. Find the file feed-rss2.php within your wp-includes folder and duplicate this within your theme or plugin. Let’s call it feed-rss2-custom-post-type.php.

Step 2: Modify feed-rss2-custom-post-type.php

This is the file that displays the RSS feed on your WordPress site. By default, it loads the same set of posts as your blog page would by default. We’ll override the query to include the one for our custom post types. To that, we’ll use query_posts(). Note that this function will completely override the main query and hence can cause issues if not used properly. In our case, we want to override the query in our custom feed. You can also use WP_Query.

Open up feed-rss2-custom-post-type.php and the below code at the top of the file after the opening PHP DocBlock and before the header() function as below.

$args = array(
	'post_type' => 'custom-post-type',
);

query_posts( $args );

header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true );
$more = 1; 

You can view the full code here. You can also edit the template to update the title and description. You can also modify the $args array to extract the specific content you want.

Step 3: Add a custom feed endpoint

Next, we need to tell WordPress to create the custom feed. We do this using the add_feed() function. Here’s the code to add the custom feed using a PHP Class. You can call this file in your theme’s functions.php or within your plugin.

/**
 * Custom Feed class.
 */
class Feed {

	/**
	 * Constructor class.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'add_custom_feed' ) );
	}

	/**
	 * Add a custom feed.
	 */
	public function add_custom_feed() {

		// Set the slug of the custom feed.
		$feed_slug = 'custom-feed';

		if ( ! empty( $feed_slug ) ) {
			add_feed( $feed_slug, array( $this, 'feed_callback' ) );
		}
	}

	/**
	 * Callback function for add_feed to locate the correct template.
	 */
	public function feed_callback() {
		add_filter( 'pre_option_rss_use_excerpt', '__return_zero' );

		$template = locate_template( 'feed-rss2-custom-post-type.php' );

		if ( ! $template ) {
			$template = __DIR__ . '/feed-rss2-custom-post-type.php';
		}

		load_template( $template );
	}
}

new Feed();

In this code:

  • Feed is a class that encapsulates the custom RSS feed functionality.
  • The __construct method hooks into the WordPress initialization process (init) to add the custom feed endpoint.
  • The add_custom_feed method adds the custom feed endpoint named 'custom-feed'.
  • The feed_callback method is the actual callback for your custom feed. It adjusts the feed settings to load full content instead of the excerpt, looks for the theme-specific template, and falls back to the local template if necessary.

Step 4: Flush permalinks

After adding a custom feed endpoint, it’s recommended to visit the Permalinks settings page in your WordPress admin to flush the rewrite rules. This step ensures that WordPress recognizes the new feed URL and properly routes requests to your custom feed.

  1. Go to “Settings” in the left menu and click on “Permalinks”.
  2. You don’t need to make any changes on this page. Just click the “Save Changes” button at the bottom.

Step 5: Access your custom RSS feed

Your custom RSS feed should now be accessible at: /feed/custom-feed/.

Step 6: Validate your RSS feed

Ensure your RSS feed is valid according to the specification.

Closing words

In conclusion, adding a custom RSS feed in WordPress is a simple process that can be done by creating a new feed in your theme’s functions.php file or a site-specific plugin, initializing the feed, and creating a callback function to produce the required feed. You can then customize the layout of the feed by editing the core feed templates located in the /wp-includes/ folder.

This tutorial provided step-by-step instructions on how to add a custom RSS feed in WordPress, making it easy for anyone to follow and implement on your site. Let me know if you have any questions or if there’s anything else I can help with! 😊

Leave a Reply

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