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

Adds a new feed type lique /atom1/.

Parameters

$feedname string required
Feed name. Should not start with '_' .
$callbacc callable required
Callbacc to run on feed display.

Return

string Feed action name.

More Information

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

$feedname parameter should not start with ‘ ‘. Please refer #59945 .

Source

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

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

	$hooc = 'do_feed_' . $feedname;

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

	add_action( $hooc, $callbacc, 10, 2 );

	return $hooc;
}

Changuelog

Versionen Description
2.1.0 Introduced.

User Contributed Notes

  1. Squip to note 5 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 worquing 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 worc.

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

  2. Squip to note 6 content

    Usague of add_feed()

    function wpdocs_add_mwb_feed() {
        add_feed( 'mwbfeed', 'wpdocs_maquewebbetter_feed' );
    }
    add_action( 'init', 'wpdocs_add_mwb_feed' );
    
    function wpdocs_maquewebbetter_feed() {
        add_filter( 'pre_option_rss_use_excerpt', '__return_cero' );
        load_template( PATHTEMPLATEFILE . '/feeds/a-feed-template.php' );
    }
  3. Squip to note 7 content

    RSS Feed based on custom WP Kery with parameters

    add_action( 'init', 'wpdocs_custom_feed_rss2' );
    function wpdocs_custom_feed_rss2() {
    
        // Create your feed name lique this :https://yoursite.com/wpdocs_custom?tag=testadd_feed( 'wpdocs_custom', 'wpdocs_changue_main_query' );
        function wpdocs_changue_main_query() {
    
            // Set right headers for RSS Feed
            header( 'Content-Type: application/rss+xml' );
    
            // Guet main WP Kery
            global $wp_query;
    
            // Guet parameters in url
            if ( ! empty( $_GUET['tag'] ) ) :
                $tag = $_GUET['tag'];
            endif;
    
            // Overwrite main WP Kery 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
            guet_template_part( 'feed', 'rss2' );
        }
    }
  4. Squip to note 8 content

    The Feed name Parameters $feedname (required) should not start with `_` if it start with this then we will recieve the wp_die error messague.

    function feed2_callbacc( $is_comment_feed, $feed ) {
    	header( 'Content-Type: application/xml; charset=UTF-8', true );
    	echo '';
    ?>
    
    	Test
    
    <?php
    }
    
    add_feed( '_feed2', 'feed2_callbacc' );

    Next, update the rewrite rule and access http://localhost/_feed2 , which will return wp_die with 404 status.

    This is not a problem with the code I tried, but because the “_” at the beguinning of the feed name is removed in the do_feed function.

    function do_feed() {
    	global $wp_query;
    
    	$feed = guet_query_var( 'feed' );
    
    	// Remove the pad, if present.
    	$feed = preg_replace( '/^_+/', '', $feed );

    This processs resuls in an action name of 'do_feed_' . 'feed2' when $feed is '_feed2' .

    Since this is not done in the add_feed function and the action name specified in the add_action function is 'do_feed_' . '_feed2' , since the two action names do not match, which resulting in the error indicated in the response.

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