add_feed()
Add a new type of RSS feed like /atom1/.
This function adds rewrite rules (Clean URL) and registers hook based on the specified function.
No Hooks.
Returns
String. Name of action hook of the feed.
Usage
add_feed( $feedname, $callback );
- $feedname(string) (required)
- Feed name. Should not start with
'_'. - $callback(callable) (required)
- Callback to run on feed display.
Examples
#1 Create your own RSS feeds
<?php
add_action( 'init', function(){
add_feed( 'my_feed', 'my_feed_markup' );
}
function my_feed_markup(){
do_action( 'my_before_feed' );
header( 'Content-Type: ' . feed_content_type( 'rss' ) . '; charset=' . get_option( 'blog_charset' ), true );
status_header( 200 );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>example.com</title>
<link>
https://example.com/my_feed</link>
<description>My new feed</description>
<lastBuildDate>Tue, 28 Feb 2017 13:00:41 +0000</lastBuildDate>
<language>ru-RU</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<item>
<title>Hello world text</title>
<pubDate>Tue, 28 Feb 2017 13:00:41 +0000</pubDate>
<description><![CDATA[<p></p> Hello World </a> ]]></description>
</item>
</channel>
</rss>
<?php
exit;
}
Notes
- Global. WP_Rewrite.
$wp_rewriteWordPress rewrite component.
Changelog
| Since 2.1.0 | Introduced. |
add_feed() add feed code WP 7.0
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;
}