Using WordPress ‘feed_content_type()’ PHP function

The feed_content_type() WordPress PHP function provides the content type for a specified feed type.

Usage

Let’s imagine you want to get the content type of an ‘rss2’ feed. Here’s how you’d do it:

$content_type = feed_content_type('rss2');
echo $content_type; 

This will output: ‘application/rss+xml’.

Parameters

  • $type (string) Optional – Type of feed. Possible values include ‘rss’, ‘rss2’, ‘atom’, and ‘rdf’. Default value is ” (an empty string).

More information

See WordPress Developer Resources: feed_content_type()

This function was first implemented in WordPress version 2.8.0 and is not deprecated as of the latest version. The source code is located in wp-includes/feed.php.

Related functions include get_feed_link(), fetch_feed(), and get_comment_feed_link().

Examples

Get ‘atom’ Feed Content Type

Here’s how you can get the content type of an ‘atom’ feed.

$content_type = feed_content_type('atom');
echo $content_type; 

This will output ‘application/atom+xml’.

Using Default Feed Type

In case you don’t specify the feed type, it will return the content type for ‘rss’ by default.

$content_type = feed_content_type();
echo $content_type; 

This will output ‘application/rss+xml’.

Get ‘rdf’ Feed Content Type

To get the content type for an ‘rdf’ feed, use the following code:

$content_type = feed_content_type('rdf');
echo $content_type; 

This will output ‘application/rdf+xml’.

Get ‘rss’ Feed Content Type

The following code snippet retrieves the content type for an ‘rss’ feed.

$content_type = feed_content_type('rss');
echo $content_type; 

This will output ‘application/rss+xml’.

Handling Invalid Feed Type

If you supply an invalid feed type, the function will return ‘text/xml’ as the default content type.

$content_type = feed_content_type('invalid_type');
echo $content_type; 

This will output ‘text/xml’.