WordPress at a glance

add_feed() WP 2.1.0

Add a new type of RSS feed like /atom1/.

This function adds rewrite rules (Clean URL) and registers hook based on the specified function.

  • Global. WP_Rewrite. $wp_rewrite

No Hooks.

Return

String. Name of action hook of the feed.

Usage

add_feed( $feedname, $function );
$feedname(string) (required)
Feed name.
$function(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>site.ru</title>
			<link>
			https://site.ru/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;
}

Code of add_feed: wp-includes/rewrite.php VER 5.0.1

<?php
function add_feed( $feedname, $function ) {
	global $wp_rewrite;

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

	$hook = 'do_feed_' . $feedname;

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

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

	return $hook;
}

Related Functions

From category: Feeds

No comments
    Hello, !     Log In . Register