add_feed( string $feedname, callable $callback ): string

Adds a new feed type like /atom1/.

Parameters

$feednamestringrequired
Feed name.
$callbackcallablerequired
Callback to run on feed display.

Return

string Feed action name.

More Information

Requires one-time use of flush_rules() to take effect.

Source

function add_feed( $feedname, $callback ) {
	global $wp_rewrite;

	if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) {
		$wp_rewrite->feeds[] = $feedname;
	}

	$hook = 'do_feed_' . $feedname;

	// Remove default function hook.
	remove_action( $hook, $hook );

	add_action( $hook, $callback, 10, 2 );

	return $hook;
}

Changelog

VersionDescription
2.1.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    When a new custom feed is added, the endpoint will render using a `Content-Type: application/octet-stream; charset=UTF-8` by default. It would be useful to document working with content types in combination with add_feed() .

    For example either:

    function add_custom_feed() {
    	add_feed( 'custom', 'render_custom_feed' );
    }
    add_action( 'init', 'add_custom_feed' );
    
    function render_custom_feed() {
    	header( 'Content-Type: application/rss+xml' );
    	echo 'aye!';
    }

    or:

    function add_custom_feed() {
    	add_feed( 'custom', 'render_custom_feed' );
    }
    add_action( 'init', 'add_custom_feed' );
    
    
    function custom_feed_content_type( $content_type, $type ) {
    	if( 'custom' == $type ) {
    		$content_type = 'application/rss+xml';
    	}
    	return $content_type;
    }
    add_filter( 'feed_content_type', 'custom_feed_content_type', 10, 2 );
    
    function render_custom_feed() {
    	echo 'aye!';
    }

    will work.

    See: https://core.trac.wordpress.org/ticket/36334

  2. Skip to note 5 content

    Usage of add_feed()

    function wpdocs_add_mwb_feed() {
        add_feed( 'mwbfeed', 'wpdocs_makewebbetter_feed' );
    }
    add_action( 'init', 'wpdocs_add_mwb_feed' );
    
    function wpdocs_makewebbetter_feed() {
        add_filter( 'pre_option_rss_use_excerpt', '__return_zero' );
        load_template( PATHTEMPLATEFILE . '/feeds/a-feed-template.php' );
    }
  3. Skip to note 6 content

    RSS Feed based on custom WP Query with parameters

    add_action( 'init', 'wpdocs_custom_feed_rss2' );
    function wpdocs_custom_feed_rss2() {
    
        // Create your feed name like this : https://yoursite.com/wpdocs_custom?tag=test
        add_feed( 'wpdocs_custom', 'wpdocs_change_main_query' );
        function wpdocs_change_main_query() {
    
            // Set right headers for RSS Feed
            header( 'Content-Type: application/rss+xml' );
    
            // Get main WP Query
            global $wp_query;
    
            // Get parameters in url
            if ( ! empty( $_GET['tag'] ) ) :
                $tag = $_GET['tag'];
            endif;
    
            // Overwrite main WP Query with yours
            $wp_query = new WP_Query(
                array(
                    'post_type' => 'any',
                    'fields'    => 'ids',
                    'tag'       => $tag,
                )
            );
    
            // Use the basic template to load your custom RSS Feed
            get_template_part( 'feed', 'rss2' );
        }
    }

You must log in before being able to contribute a note or feedback.