Using WordPress ‘do_feed_rss2()’ PHP function

The do_feed_rss2() WordPress PHP function loads either the RSS2 comment feed or the RSS2 posts feed.

Usage

To use this function, simply pass it a boolean value. If true, it will load the comment feed; if false, it will load the normal posts feed.

do_feed_rss2(false); // This will load the RSS2 posts feed

Parameters

  • $for_comments (bool): This is a required parameter. Passing true will load the RSS2 comment feed, while false will load the RSS2 posts feed.

More information

See WordPress Developer Resources: do_feed_rss2()

The do_feed_rss2() function uses the load_template() function under the hood. Always ensure to use updated version of WordPress as deprecated functions may not work properly.

Examples

Loading the RSS2 Posts Feed

In this example, we’ll load the RSS2 posts feed by passing false to the function. This can be useful when you want to display your latest blog posts in an RSS reader.

// Load the RSS2 posts feed
do_feed_rss2(false);

Loading the RSS2 Comment Feed

In this example, we’re loading the RSS2 comment feed by passing true to the function. This can be useful when you want to keep track of the latest comments on your site through an RSS reader.

// Load the RSS2 comment feed
do_feed_rss2(true);

Conditional Loading of Feeds

Let’s say you want to conditionally load the feeds based on some condition, you can use this function inside a conditional statement.

if ( $condition ) {
    // Load the RSS2 posts feed
    do_feed_rss2(false);
} else {
    // Load the RSS2 comment feed
    do_feed_rss2(true);
}

Using with a Variable

You can also use this function with a variable, which can be dynamically set based on your application’s logic.

$load_comments = get_user_option('load_comment_feed');

// Load the appropriate feed
do_feed_rss2($load_comments);

Inside a Function

This example demonstrates using do_feed_rss2() inside a custom function. This can be useful for organizing your code or customizing behavior.

function custom_load_feed($for_comments) {
    // Load the appropriate feed
    do_feed_rss2($for_comments);
}

// Now you can use your custom function
custom_load_feed(false);