Adds a new feed type like /atom1/.
Parameters
$feedname
stringrequired- Feed name.
$callback
callablerequired- Callback to run on feed display.
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
Version | Description |
---|---|
2.1.0 | Introduced. |
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:
or:
will work.
See: https://core.trac.wordpress.org/ticket/36334
Usage of add_feed()
RSS Feed based on custom WP Query with parameters
The Feed name Parameters $feedname(required) should not start with `_` if it start with this then we will recieve the wp_die error message.
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 beginning of the feed name is removed in the do_feed function.
This process results 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 theadd_action
function is'do_feed_' . '_feed2'
, since the two action names do not match, which resulting in the error indicated in the response.