Using WordPress ‘do_feed()’ PHP function

The do_feed() WordPress PHP function loads the feed template using an action hook. If the feed action does not have a hook, the function will halt with a message indicating that the feed is not valid. It is advisable to have only one hook for each feed.


Here’s a general usage of the do_feed() function:


Since do_feed() has no parameters, it’s straightforward to use. Upon execution, it loads the appropriate feed template or displays a message if the feed is not valid.


The do_feed() function does not accept any parameters.

More information

See WordPress Developer Resources: do_feed()

The do_feed() function has been a part of WordPress since version 2.1. It’s a core function of WordPress, so it is not deprecated. The source code for this function can be found in wp-includes/functions.php.


Basic usage of do_feed()

// Call the function

In this example, do_feed() will load the feed template associated with the current action hook. If no such hook exists, it will display an error message.

Using do_feed() in a template file

// In a template file, for example single.php
if (have_posts()) {
    while (have_posts()) {
// At the end of your loop

In this example, do_feed() is placed at the end of a loop in a template file, which will load the feed template after the posts are displayed.

Using do_feed() in a custom function

// Create a custom function
function my_custom_function() {
    // Some code here...
    // More code here...
// Call your custom function

Here, do_feed() is used within a custom function. When my_custom_function() is called, it runs do_feed() as part of its operations.

Using do_feed() in an action hook

// Add an action hook for 'init'
add_action('init', 'do_feed');

In this case, do_feed() is added to the ‘init’ action hook. This means that do_feed() will be called during WordPress initialization.

Using do_feed() in a plugin

// In your plugin file
function my_plugin_function() {
    // Plugin code here...
    // More plugin code here...
// Call your plugin function

In this example, do_feed() is used within a plugin function. This can be useful for plugin development where you need to load the feed template as part of the plugin’s functionality.