Feeds (RSS) in WordPress
Feeds in WordPress are a format for displaying posts or comments in a special format. It's the same as displaying posts on the main page, but in a special format that is created according to certain standards and can be understood by a news aggregator program.
Feeds are used, for example, by a program that collects the latest publications from different sites, including your site (your feed). In order for this program to understand where the post title is and where the content is, the feeds of all sites have the same format - this is a widely accepted standard. There are several such standards: RSS, RSS2, Atom, RDF. Lately, almost always RSS2 is used.
In previous versions of WordPress, the feed format RSS-0.92 was generated, but now this format redirects to RSS-2.0. However, the template of this old format is still present in the core.
-
To work with feeds, see the function fetch_feed()
-
See examples of changing feeds in the collection of hooks for feeds here.
- Validate feed: https://validator.w3.org/feed/
How Feeds Work in WordPress
First of all, it should be noted that the feeds of the current WordPress site are not cached. Caching occurs in the browser, to bypass it, refresh the page with Ctrl + F5. Or you can add such a hook:
## Disable caching in the browser for feed requests add_filter( 'wp_headers', function($headers){ if( !empty($GLOBALS['wp']->query_vars['feed']) ){ unset( $headers['ETag'], $headers['Last-Modified'] ); } return $headers; });
How the feed request works
After loading the entire WP and setting the current request and its parameters, the file template-loader.php is called, in it, after checking is_feed(), the function do_feed() is called.
Then, if a specific feed type was not specified in the request, it is determined by the function get_default_feed() (you can change the default feed type through its hook).
Next, the dynamic hook do_feed_(feed) is called:
do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed );
For each variant of this hook in WP, a function is connected by default (see default-filters.php):
add_action( 'do_feed_rdf', 'do_feed_rdf', 10, 1 ); add_action( 'do_feed_rss', 'do_feed_rss', 10, 1 ); add_action( 'do_feed_rss2', 'do_feed_rss2', 10, 1 ); add_action( 'do_feed_atom', 'do_feed_atom', 10, 1 );
That is, depending on the type of feed, one of the functions is triggered, which connects the necessary template file, which in turn generates the HTML code of the feed page.
Usually, the function do_feed_rss2() is triggered:
function do_feed_rss2( $for_comments ) { if ( $for_comments ) { load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' ); } else { load_template( ABSPATH . WPINC . '/feed-rss2.php' ); } }
How to Disable Feeds in WordPress
To do this, you need to unhook all feed functions from the hooks:
remove_action( 'do_feed_rdf', 'do_feed_rdf', 10, 1 ); remove_action( 'do_feed_rss', 'do_feed_rss', 10, 1 ); remove_action( 'do_feed_rss2', 'do_feed_rss2', 10, 1 ); remove_action( 'do_feed_atom', 'do_feed_atom', 10, 1 );
Now when you visit the feed page, for example /feed
, you will see a 404 page with the message:
Since we no longer have feeds, we also need to remove the feed links from wp_head:
add_action( 'wp', function(){ remove_action( 'wp_head', 'feed_links_extra', 3 ); remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'rsd_link' ); } );
Read also: Removing feeds except for the main page feed
Content Types and Feed Formats
Now that we've covered what feeds are, let's look at the types of content that WordPress provides in feed format:
Feed Type | Example URL |
---|---|
Latest posts (main feed) | http://example.com/feed/ |
Latest comments (main feed) | http://example.com/comments/feed/ |
Comments for a specific post | http://example.com/2009/07/post-name/feed/ |
Comments for a specific page | http://example.com/archives/feed/ |
Posts for a day, month, year, category, tag | http://example.com/2010/feed/, http://example.com/2010/10/feed/, http://example.com/tag/tag-name/feed/ |
Posts for a specific search query | http://example.com/search/term/feed/ |
Instead of /feed/ at the end, other types of RSS can also be used:
- Atom – /atom/
- RDF – /rdf/
- RSS2 – /feed/, /feed/rss/, or /feed/rss2/
URLs (links) of Feeds in WordPress
Pretty Permalinks for Feeds of All Posts
- http://example.com/feed/ (RSS 2.0 format)
- http://example.com/feed/rss2/ (RSS 2.0 format)
- http://example.com/feed/rss/ (RSS 0.92 format)
- http://example.com/feed/rdf/ (RDF/RSS 1.0 format)
- http://example.com/feed/atom/ (Atom format)
Non-Pretty Permalinks for Feeds of All Posts
- http://example.com/wp-rss2.php (RSS 2.0 format)
- http://example.com/wp-rss.php (RSS 0.92 format)
- http://example.com/wp-rdf.php (RDF/RSS 1.0 format)
- http://example.com/wp-atom.php (Atom format)
Non-Pretty Permalinks for Feeds of All Posts via Query Parameter
- http://example.com/?feed=rss2 (RSS 2.0 format)
- http://example.com/?feed=rss (RSS 0.92 format)
- http://example.com/?feed=rdf (RDF/RSS 1.0 format)
- http://example.com/?feed=atom (Atom format)
In which option each feed link is stored
When you need to get the link to the default feed of a specific type, you can use the site options functions (site information):
<?php bloginfo('rss2_url'); ?> <!-- RSS 2.0 format --> <?php bloginfo('rss_url'); ?> <!-- RSS 0.92 format --> <?php bloginfo('rdf_url'); ?> <!-- RDF/RSS 1.0 format --> <?php bloginfo('atom_url'); ?> <!-- Atom format -->
Links for the Feed of All WordPress Comments
- http://example.com/comments/feed/ (Pretty Permalink format)
- http://example.com/wp-commentsrss2.php (default format)
- http://example.com/?feed=commentsrss2 (query parameter format)
Where the link to the feed of all comments is stored:
<?php bloginfo('comments_rss2_url'); ?>
Link to Comments of a Specific Post
<?php comments_rss_link('Subscribe to comments on this post via the RSS-2.0 feed'); ?>
Another way to display the link to the comments feed of a specific post is to add /feed/ to the end of the permalink (for Pretty Permalinks) or ?feed=rss2 (if Pretty Permalinks are disabled). That is, the result will be:
- http://example.com/individual-post/feed/ (Pretty Permalink format)
- http://example.com/individual-post/?feed=rss2 (default format)
The link to the post's comments feed can also look like this if pretty URLs (permalinks) are disabled. Below, p is the post ID.:
- http://example.com/?feed=rss2&p=123
Links to Feeds of Posts from a Category
- http://example.com/category/categoryname/feed/ (Pretty Permalink format)
- http://example.com/wp-rss2.php?cat=33 (default format)
PHP Templates for WordPress Feeds (Files)
Which files of the engine are responsible for outputting which feed.
Post Feeds:
- /wp-includes/feed-atom.php
- /wp-includes/feed-rdf.php
- /wp-includes/feed-rss.php
- /wp-includes/feed-rss2.php
Each file is responsible for its feed type.
feed-rss.php is considered an outdated format.
The ending /feed refers to the file feed-rss2.php.
Comment Feeds:
Feed Functions
See the file: /wp-includes/feed.php