add_feed() wordpress function and usage

Home / WordPress / WordPress Wiki / add_feed() wordpress function and usage

add_feed( string $feedname, callable $function )
Add a new feed type like /atom1/.

Parameters

$feedname

(string) (Required) Feed name.

$function

(callable) (Required) Callback to run on feed display.

Return

(string) Feed action name.

More Information

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

Sample Usage

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments