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/ |
- 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)
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
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
- http://digwp.com/comments/feed/rdf/
- http://digwp.com/2012/09/notification-list-for-new-books/feed/rdf/
/wp-includes/feed.php
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 atDigWP.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:- Create a custom page template – and paste into it any existing feed template
- 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.
- Provide a link to the feed – once everything is in place, the custom feed will be available at the URL of its custom page.
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.
Customizing feeds with filters and hooks
The most direct way of customizing WordPress feeds is to hook into them directly withactions 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 © '.date('Y').' '.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_ns()
rdf_header()
rdf_item()
rss2_ns()
rss2_head()
rss_enclosure()
rss2_item()
atom_ns()
atom_comments_ns()
comments_atom_head()
comment_atom_entry()
rss2_ns()
rss2_comments_ns()
commentsrss2_head()
commentrss2_item()
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’sfunctions.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 firstreturn
, 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 🙂Revisions
- June 11, 2013 @ 02:11:23 [Current Revision] by PeterLugg
- June 11, 2013 @ 02:16:30 [Autosave] by PeterLugg
- June 11, 2013 @ 02:11:20 by PeterLugg
Revision Differences
There are no differences between the June 11, 2013 @ 02:11:20 revision and the current revision. (Maybe only post meta information was changed.)
No comments yet.