DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Customizing WordPress Feeds

Posted by on

WordPress feeds enable your visitors to subscribe to your content for use in their favorite feed-reader. For example, subscribing to the main-posts feed and/or the comments feed is a great way for your readers to stay current with all the latest from your site.

With WordPress, you can deliver a wide variety of “Full-text” or “Summary” (partial) feeds in numerous formats, including Atom, RDF, and RSS2. This variety extends the reach of your content by enabling your feeds to be read in more apps, readers, and devices.

As awesome as the default feeds may be, they are also readily customizable using a variety of methods. In addition to WP‘s built-in ways of configuring your feeds, you can go even further with custom templates, functions, and plugins. In this DigWP post, you’ll learn everything you need to customize your feeds with bonus content, recent posts, social media, and much more.

Contents

Overview of WordPress feeds

By default WordPress generates a feed for just about everything, as seen in this overview:

Feed type Description Example*
All Posts Main content feed – includes your latest posts http://digwp.com/feed/
All Comments Main comments feed – includes latest comments http://digwp.com/comments/feed/
Individual Posts Includes latest comments on a specific post http://digwp.com/2009/07/getting-more-fine-grained-with-includes/feed/
Individual Pages Includes latest comments on a specific page http://digwp.com/archives/feed/
Archives Day, month, year – includes latest posts in each archive http://digwp.com/2010/feed/, http://digwp.com/2010/10/feed/, http://digwp.com/2010/10/02/feed/
Search Results Includes latest posts for a particular search query http://digwp.com/search/term/feed/

* Note: examples show feed URLs when WP permalinks are enabled.

As if that many feeds weren’t enough, each type of feed is available is the following formats:

  • Atom – append /atom/ to any type of feed
  • RDF – append /rdf/ to any type of feed
  • RSS2 – append /feed/, /feed/rss/, or /feed/rss2/ to just about any WP-generated page (this is the default feed format)

For more details, here’s a complete guide to all of the different WordPress feed URLs.

Note: In previous versions of WordPress feeds were also generated in RSS-0.92 format, but those now are redirected to their respective RSS-2.0 formats, even though the template file for RSS-0.92 still exists in the core. The feed template files are what WordPress uses to generate the various types of feeds in their various formats. Let’s take a look..

Feed Template files

To generate the different feed formats — Atom, RDF, and RSS2 — for its myriad feed types, WordPress employs the following template files:

Post-based feeds

  • /wp-includes/feed-atom.php
  • /wp-includes/feed-rdf.php
  • /wp-includes/feed-rss.php
  • /wp-includes/feed-rss2.php

The post-based feeds look self-explanatory: feed-atom.php is used for Atom feeds, feed-rdf.php for RDF feeds, and so on. As mentioned, the thing to note here is that the feed-rss.php (for the RSS-0.92 format) still exists in the WP core, even though it’s no longer used, afaik.

Comment-based feeds

  • /wp-includes/feed-atom-comments.php
  • /wp-includes/feed-rss2-comments.php

The comment-based feeds are available in only two flavors: Atom and RSS 2.0. As with post feeds, requests for RSS-0.92 comment feeds are redirected to their respective RSS-2.0 formats. Also worth noting, requests for RDF-formatted comment feeds are mysteriously redirected to RSS2 post feeds. Examples:

For each of these examples, it would make more sense to redirect to the respective RSS2-formatted comments feed, rather than to any of the post feeds.

Feed functions

  • /wp-includes/feed.php

Lastly, the feed.php file contains some of the key functions used in the various feed-templates. For example, the rss_enclosure() function is used in feed-rss2.php to display any RSS enclosure(s) included for the current post. So, if you’ve added, say, the following enclosure to your latest post (via custom field named “enclosure”):

<enclosure url="http://awesome-video.flv" length="104857600" type="video/x-flv" />

WordPress will automatically insert the enclosure into your RSS2 feed via the rss_enclosure() function. Here is what your feed’s XML will look like after the enclosure is included (line breaks added for emphasis):

<item>
	<title>Awesome Video</title>
	<link>http://example.com/awesome-video/</link>
	<comments>http://example.com/awesome-video/#comments</comments>
	<pubDate>Wed, 17 Oct 2012 06:51:07 +0000</pubDate>
	<dc:creator>Perishable</dc:creator>
	<category><![CDATA[WordPress]]></category>
	<guid isPermaLink="false">http://example.com/?p=132</guid>
	<description><![CDATA[Check out this awesome video..]]></description>
	<wfw:commentRss>http://example.com/awesome-video/feed/</wfw:commentRss>
	<slash:comments>500</slash:comments>

	<enclosure url="http://awesome-video.flv" length="104857600" type="video/x-flv" />

</item>

And here’s what we see near the end of the feed-rss2.php template that makes it happen:

.
.
.
		<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
		<slash:comments><?php echo get_comments_number(); ?></slash:comments>

<?php rss_enclosure(); ?>

	<?php do_action('rss2_item'); ?>
	</item>
	<?php endwhile; ?>
</channel>
</rss>

There is a similar function for including enclosures for Atom feeds, atom_enclosure, which is also included in feed.php. And so, now that we’re all up to speed on the what, why and where, let’s apply the information as we learn how to customize our own WordPress feeds.

Built-in ways to customize feeds

The easiest way to customize your feeds is by using the built-in options that WordPress provides. Probably these are familiar to most of us, but they’re worth mentioning along with some of their pros and cons:

Full-text or Summary feed?

If you visit “Settings” > “Reading Settings”, you can choose whether to syndicate your complete posts or only the excerpt. Note: does not apply to comment feeds, which always display comments in their entirety. Delivering partial-feeds is a good way to prevent content-scraping, while delivering full-feeds makes it easier for your readers to stay current (i.e., they don’t have to visit your site, which may be another reason to use partial feeds).

Number of posts/comments to include in feeds

To customize the number of items that appear in your post and comment feeds, visit “Settings” > “Reading Settings” and adjust the option, “Syndication feeds show the most recent”. The number that you choose will apply to both post and comment feeds.

Including larger numbers of posts can make your feed look more attractive to potential subscribers, which may be useful when delivering partial feeds. When delivering full-feeds, however, you may be better served by choosing a lower number of feed items. Here at DigWP.com, we deliver full-text feeds, eight at a time.

Character encoding for feeds

While the default UTF-8 character-encoding is recommended, it is possible to choose an alternate encoding method. Visit “Settings” > “Reading Settings” > “Encoding for pages and feeds” to make it happen.

Customize with custom templates

As explained in previous posts, a great way to generate custom feeds is to use a custom feed template. Those posts explain it all in detail, with the basic idea going something like this:

  1. Create a custom page template – and paste into it any existing feed template
  2. Modify the custom feed template – for example, change the number of posts, make it a category-specific feed, add custom XML enclosures, and so on.
  3. Provide a link to the feed – once everything is in place, the custom feed will be available at the URL of its custom page.

As you can imagine, using custom feed templates is an ideal approach, but it’s also possible to modify your existing feeds (instead of creating new ones). Read on to learn how..

Customizing with WP plugns

The easiest way to go further than default WP customization options is to use a plugin. There are many available. Here are some of our favorites that show the wide range of feed-customizing that’s possible with free WordPress plugins:

  • Comments On Feed – enables visitors to view and leave comments directly from each post in the WordPress content feed.
  • Feed Template Customize – enables you to modify WP’s RSS and Atom feeds using your own custom feed-templates.
  • RSS Image Feed – adds the first image of a post to your feeds, even in Firefox and even if you only display the excerpt.
  • RSS Custom Fields – includes all of your custom fields in your feed so you can pull data out of WordPress and use it elsewhere.
  • Custom Post Type RSS feeds – makes it “super easy” to create an RSS feed based on custom post types.

And there’s many more feed-tweaking plugins available in the WordPress Plugin Directory and elsewhere. Knock yourself out :)

Customizing feeds with filters and hooks

The most direct way of customizing WordPress feeds is to hook into them directly with actions and filters. For example, WP hooks make it easy to add custom content:

// add custom feed content
function add_feed_content($content) {
	if(is_feed()) {
		$content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>';
	}
	return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');

With that snippet included in your theme’s functions.php file, WordPress will display a line of copyright information (for example) to each of your feed items. Of course, much more is possible once you’re able to target hooks for specific feeds. Fortunately, WordPress provides some useful format-specific feed hooks:

Atom post feeds

  • atom_ns()
  • atom_head()
  • atom_author()
  • atom_enclosure()
  • atom_entry()

RDF post feeds

  • rdf_ns()
  • rdf_header()
  • rdf_item()

RSS2 post feeds

  • rss2_ns()
  • rss2_head()
  • rss_enclosure()
  • rss2_item()

Atom comment feeds

  • atom_ns()
  • atom_comments_ns()
  • comments_atom_head()
  • comment_atom_entry()

RSS2 comment feeds

  • rss2_ns()
  • rss2_comments_ns()
  • commentsrss2_head()
  • commentrss2_item()

For more information (and more hooks), and to see where everything is located, check out the relatively short feed template files mentioned previously. Some of these hooks are also discussed in the WP Codex, and there are some additional infos elsewhere on the Internets. For now, we’ve seen how to hook into feeds and some feed-specific hooks to use, so let’s continue with some practical examples of customizing feeds via your theme’s functions.php.

Examples of customizing feeds via functions.php

Straight on then, here are some practical examples showing different ways to customize default WordPress feeds directly from your theme.

Add a custom logo and icon to your feed

As explained here, a useful way to boost your brand is to include a custom logo and icon to the header area of all of your feeds. Just add the following code to your theme’s functions.php file:

// add icon and logo to Atom feeds
add_action('atom_head','digwp_atom_feed_add_icon');
add_action('comments_atom_head','digwp_atom_feed_add_icon');
function digwp_atom_feed_add_icon() { ?>

	<feed>
		<icon><?php echo get_template_directory_uri(); ?>/images/logo.ico</icon>
		<logo><?php echo get_template_directory_uri(); ?>/images/logo.gif</logo>
	</feed>

<?php }

// add icon and logo to RSS feeds
add_action('rss_head','digwp_rss_feed_add_icon');
add_action('rss2_head','digwp_rss_feed_add_icon');
add_action('commentsrss2_head','digwp_rss_feed_add_icon');
function digwp_rss_feed_add_icon() { ?>

	<image>
		<url><?php echo get_template_directory_uri(); ?>/images/logo.gif</url>
		<title><?php bloginfo_rss('name'); ?></title>
		<link><?php bloginfo_rss('url'); ?></link>
		<width>125</width>
		<height>75</height>
		<description><?php bloginfo('description'); ?></description>
	</image>

<?php }

// add icon and logo to RDF feeds
add_action('rdf_header','digwp_rdf_feed_add_icon');
function digwp_rdf_feed_add_icon() { ?>

	<image rdf:about="<?php echo get_template_directory_uri(); ?>/images/logo.gif">
		<title><?php bloginfo_rss('name'); ?></title>
		<url><?php echo get_template_directory_uri(); ?>/images/logo.gif</url>
		<link><?php bloginfo_rss('url'); ?></link>
		<description><?php bloginfo('description'); ?></description>
	</image>

<?php }

Note that there are two images in play here: an icon for the Atom feeds and a logo for the Atom, RSS/RSS2, and RDF feeds. Once both the code and images are in place, your WordPress feeds will be uniformly branded with the ultra-buff image(s) of your choice.

Add feed <link> tags to your web pages

A great way to boost feed reach is to include automatic feed links to the <head> section of your web pages. When this theme-feature is enabled, WordPress draws upon its vast army of feed types and includes links to any relevant feeds for the current page view. This enables “feed-aware” devices and browsers to automatically detect your available feeds, making it easier for visitors to discover and subscribe. This theme feature is enabled with the following snippet added to your theme’s functions.php file:

// enable WP automatic feed links
if (function_exists('automatic_feed_links')) {
	automatic_feed_links();
} else {
	return;
}

Nothing else to do! Just slap it in there and enjoy your new automatic feed links. Huzzah!

Completely remove the version number from pages and feeds

By default, WordPress includes its version number in your feeds. If you’re always running the most current version of WordPress, there is nothing to worry about; otherwise, it’s a good idea to remove the version information wherever possible. Here’s the code to add to functions.php:

// remove version info from head and feeds
add_filter('the_generator', 'digwp_complete_version_removal');
function digwp_complete_version_removal() {
	return '';
}

As an added bonus, this technique also removes the version information from the <head> section of your web pages.

Delay feed update after posting

Thanks to the fine folks at WP Engineer, here is a technique for delaying feed-updates for a few minutes after posting a new post or page:

// delay feed update after posting
add_filter('posts_where', 'publish_later_on_feed');
function publish_later_on_feed($where) {
	global $wpdb;
	if (is_feed()) {
		// timestamp in WP-format
		$now = gmdate('Y-m-d H:i:s');

		// value for wait; + device
		$wait = '5'; // integer

		// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
		$device = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

		// add SQL-sytax to default $where
		$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
	}
	return $where;
}

After adding this code to your functions.php file, WordPress will wait five minutes before updating your feeds with the new content. This buys you some precious time to triple-check everything and fix any last-minute errors. To delay feed-updating for a longer (or shorter) amount of time, edit the $wait and $device variables to whatever you prefer.

Disable all feeds

With all this talk about customizing feeds, it’s worth asking whether or not you want to make feeds available in the first place. They’re enabled by default, but sometimes it’s optimal to disable them entirely or even selectively. Here’s the function to make it happen:

// disable all feeds
function digwp_disable_feed() {
	wp_die(__('<h1>Feed not available, please visit our <a href="'.get_bloginfo('url').'">Home Page</a>!</h1>'));
}
add_action('do_feed',      'digwp_disable_feed', 1);
add_action('do_feed_rdf',  'digwp_disable_feed', 1);
add_action('do_feed_rss',  'digwp_disable_feed', 1);
add_action('do_feed_rss2', 'digwp_disable_feed', 1);
add_action('do_feed_atom', 'digwp_disable_feed', 1);

Note: Only use this function if you want to disable your feeds! Or to only disable certain feeds, comment out or delete its corresponding add_action and you’re good to go.

Customize the only first post in feeds

For our last example, here is a way to target and customize only the first post in your various feeds. Add the following code to functions.php:

// customize first feed post only
add_filter('the_content', 'digwp_customize_first_feed_post');
add_filter('the_excerpt', 'digwp_customize_first_feed_post');
function digwp_customize_first_feed_post($content) {
	global $wp_query;
	if (is_feed()) {
		$feed_type = get_query_var('feed');
		if ($wp_query->current_post == 0) {
			return '<p>Put some custom content here!</p>' . $content;
		} else {
			return $content;
		}
	} else { return $content; }
}

This technique works out of the box to add some custom content (or whatever) to the first feed item, and is extendible to target subsequent posts as well. Feel free to customize the output to be anything that makes sense. Note: as-is, the custom content is added to the beginning of the first post. To add content instead to the end of the first post, switch the order of the first return, like so:

return $content . '<p>Put some custom content here!</p>';

Incidentally, I use this technique in my new feed-tracking plugin to add a custom-tracking image — works a treat!

Conclusion

We hope this article helps people in customizing their own WordPress feeds. The take-home message is that WordPress provides a wealth of possibilities when it comes to creating, formatting, and customizing your feeds. So be original, and have some fun :)

6 Responses

  1. Thank’s jeef i need this.
    Btw, I use custom post types are calling all feed on the homepage:

    function mytheme_request($am) {
         if (isset($am['feed']) && !isset($am['post_type']))
              $am['post_type'] = array('post', 'portfolio', 'blogazine');
         return $am;
    }
    add_filter('request', 'mytheme_request');

  2. Very handy and well done, thanks Jeff. What I need is a feed showing only new comments (compared to the last time the feed was updated).

    I have a feeling that, if there is no ready made plugin that already does this, it can only be done via hooks and filters… unless you have any other suggestions?

  3. Nice step by tutorial with snapshots, Really helpful for newbie.

  4. tnx for the tips @Jeff. looking froward to build custom feed for my site.

  5. Wow, awesome write up! Feeds are one of those mysterious topics that I was struggling to find any decent info about.

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry