You can take a look at
ABSPATH . WPINC . 'feed-*.*';
to see examples. WP core has feed tempaltes for the following protocols:
- Atom
- RDF
- RSS
- RSS2
When writing a plugin, you can take the following quick test plugin as base. It adds a feed name custom-feed
to the list of feeds.
<?php
/* Plugin Name: (WPSE) #172796 Custom Feed */
add_action( 'init', function()
{
add_feed( 'custom-feed', function()
{
/** @var \WP_Query $wp_query */
global $wp_query;
// Custom conditions when to output the feed - and when not
if ( 'foo' !== get_query_var( 'name' ) )
return;
load_template( plugin_dir_path( __FILE__ ).'templates/feed-rss2.php' );
} );
} );
To get a quick look at the default feeds, simply dump them
var_dump( $GLOBALS['wp_rewrite']->feeds );
When you activate above test plugin, you should find custom-feed
as last entry in the array.
Inside any callbacks you should check if you are actually working on the new custom feed:
$wp_query->is_feed( [ 'custom-feed', ] );
To get a valid feed, you will have to set a header
. Core does it like the following:
header( sprintf(
'Content-Type: %s; charset=%s',
feed_content_type( 'rss-http' ),
get_option( 'blog_charset' )
), true );
Then there’s the actual XML opening tag:
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
after that, there’s a place to allow plugins to hook in:
do_action( 'rss_tag_pre', 'rss2' );
Finally there is the <rss version="2.0" ...
tag opening and another hook to add custom namespaces
do_action( 'rss2_ns' );
The list goes on and on. The best you can do is to take the core files as blueprint for your own feeds (copy/paste them to your plugin and alter the output).