Using WordPress ‘get_default_feed()’ PHP function

The get_default_feed() WordPress PHP function retrieves the default feed. It usually returns ‘rss2’, but a plugin may modify it through the ‘default_feed’ filter.

Usage

Here’s a simple way to use the function:

$default_feed = get_default_feed();
echo $default_feed;

The output will generally be ‘rss2’, unless modified by a plugin.

Parameters

  • This function does not require any parameters.

More information

See WordPress Developer Resources: get_default_feed()
This function is part of the core WordPress functions and isn’t deprecated. Its source code can be found in the wp-includes/functions.php file.

Examples

Displaying the default feed type

This code will print out the default feed type, typically ‘rss2’.

$default_feed = get_default_feed();
echo "The default feed type is: " . $default_feed;

Using the default feed in a conditional

This code will check if the default feed is ‘rss2’. If it is, it will print a message.

$default_feed = get_default_feed();

if ($default_feed == 'rss2') {
    echo "The default feed is RSS 2.0.";
} else {
    echo "The default feed is not RSS 2.0.";
}

Changing the feed type with a filter

This code demonstrates how a plugin can change the default feed type. We’ll set it to ‘atom’.

add_filter('default_feed', function() {
    return 'atom';
});

$default_feed = get_default_feed();
echo "The default feed type is: " . $default_feed;

This code will generate a link to the default feed.

$default_feed = get_default_feed();
echo "Visit the default feed at: " . get_bloginfo('url') . "/feed/" . $default_feed;

Adding default feed to the header

This code will add a link to the default feed in the HTML head section. This is useful for browsers and tools that detect feed links.

$default_feed = get_default_feed();
echo '<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="' . get_bloginfo('url') . "/feed/" . $default_feed . '" />';