Using WordPress ‘do_feed_atom()’ PHP function

The do_feed_atom() WordPress PHP function loads either the Atom comment feed or the Atom posts feed.

Usage

do_feed_atom( $for_comments );

For instance, if you’d like to load the comment feed, you’d call the function like this:

do_feed_atom(true);

And the function will return the Atom comment feed.

On the other hand, if you want to get the normal feed, you’d call the function this way:

do_feed_atom(false);

Parameters

  • $for_comments (bool) – Required parameter. If set to true, it loads the comment feed. If set to false, it loads the normal feed.

More information

See WordPress Developer Resources: do_feed_atom()

This function is used within the template loading system of WordPress.

Examples

Load Atom Comment Feed

Here’s how you can load the Atom comment feed:

// Load the Atom comment feed
do_feed_atom(true);

Load Atom Posts Feed

If you want to load the Atom posts feed instead, you can do it like this:

// Load the Atom posts feed
do_feed_atom(false);

Conditionally Load Feed

You can also conditionally load the feed based on a particular condition. For instance, if you want to load the comment feed only on single posts, you’d do this:

if (is_single()) {
    // Load Atom comment feed on single posts
    do_feed_atom(true);
} else {
    // Load Atom posts feed elsewhere
    do_feed_atom(false);
}

Using with Action Hook

You can also use this function with an action hook. For instance, you can hook it into ‘init’ to execute it when WordPress is fully loaded:

add_action('init', function() {
    do_feed_atom(true);
});

Load Feed Based on User Role

If you want to load a particular feed based on the user’s role, you can do something like this:

if (current_user_can('editor')) {
    // Load Atom comment feed for editors
    do_feed_atom(true);
} else {
    // Load Atom posts feed for other roles
    do_feed_atom(false);
}