How to fetch RSS feeds from external sources in WordPress

The fetch_feed function in WordPress is a built-in feature that allows the retrieval of RSS or Atom feeds from a specified URL. This function is part of the SimplePie library integrated into WordPress, enabling the handling and parsing of feed data.

By utilizing the fetch_feed function, users can import external content from other websites or blogs into their WordPress site. This content can include blog posts, news articles, podcasts, videos, and more. The imported data can be displayed in a variety of ways, such as in a widget on the website’s sidebar or within a page or post. This functionality can be beneficial for displaying updated content from external sources directly on a WordPress site.

The fetch_feed function fetches the feed data and returns it as a SimplePie object. This object can then be manipulated using the SimplePie API to extract the desired information, such as the feed items, title, description, and more.

It’s important to note that the fetch_feed function handles all the HTTP requests and caching. This means that if the same feed is fetched multiple times, it does not result in multiple HTTP requests. Instead, the feed data is cached and reused, which can improve the performance of the WordPress site.

Parameters Accepted by the fetch_feed Function

The WordPress fetch_feed function accepts a single parameter:

  • $url (string|string[]): This is a mandatory parameter indicating the URL of the feed that needs to be retrieved. In case an array of URLs is provided, the function uses the multifeed feature of SimplePie to merge these feeds. For more information on this, you can refer to the SimplePie documentation.

Return Value of the fetch_feed Function

The fetch_feed function in WordPress returns either a SimplePie object or a WP_Error object. A successful execution of the function will yield a SimplePie object. However, in case of any failure in execution, the function will return a WP_Error object.

Examples

How to Display Feed Title and Permalink with fetch_feed

<?php include_once(ABSPATH . WPINC . '/feed.php');

$rss = fetch_feed('https://www.example.com/feed');

if (!is_wp_error($rss)) {
 $maxitems = $rss->get_item_quantity(5); 
 $rss_items = $rss->get_items(0, $maxitems);
}

foreach ($rss_items as $item) {
 echo '<p>Title: ' . $item->get_title() . '</p>';
 echo '<p>Link: ' . $item->get_permalink() . '</p>';
}
?>

This code snippet fetches the RSS feed from the URL ‘https://www.example.com/feed’. It uses the fetch_feed function to retrieve the feed and store it in the $rss variable. If there’s no error in fetching the feed, it gets the first 5 items from the feed and stores them in the $rss_items variable. Then it loops through each item, displaying the title and the permalink of each item in a paragraph.

How to Display Feed Content with fetch_feed

<?php include_once(ABSPATH . WPINC . '/feed.php');

$rss = fetch_feed('https://www.example.com/feed');

if (!is_wp_error($rss)) {
 $maxitems = $rss->get_item_quantity(1);
 $rss_items = $rss->get_items(0, $maxitems);
}

foreach ($rss_items as $item) {
 echo '<p>' . $item->get_content() . '</p>';
}
?>

This code snippet fetches the RSS feed from the URL ‘https://www.example.com/feed’ and displays the content of the first item in the feed. It uses the fetch_feed function to retrieve the feed and store it in the $rss variable. If there’s no error in fetching the feed, it gets the first item from the feed and stores it in the $rss_items variable. Then it displays the content of the item in a paragraph.

How to Display Feed Date with fetch_feed

<?php include_once(ABSPATH . WPINC . '/feed.php');

$rss = fetch_feed('https://www.example.com/feed');

if (!is_wp_error($rss)) {
 $maxitems = $rss->get_item_quantity(1);
 $rss_items = $rss->get_items(0, $maxitems);
}

foreach ($rss_items as $item) {
 echo '<p>Date: ' . $item->get_date('j F Y | g:i a') . '</p>';
}
?>

This code snippet fetches the RSS feed from the URL ‘https://www.example.com/feed’ and displays the date of the first item in the feed. It uses the fetch_feed function to retrieve the feed and store it in the $rss variable. If there’s no error in fetching the feed, it gets the first item from the feed and stores it in the $rss_items variable. Then it displays the date of the item in a paragraph, formatted as ‘Day Month Year | Hour:Minute AM/PM’.

Conclusion

The fetch_feed() function in WordPress is a useful tool that enables the retrieval of RSS or Atom feeds from a specified URL. This function is particularly beneficial when you need to display content from another site on your WordPress site, as it can fetch and parse the feed data into an easy-to-manipulate format. It’s important to note that the fetch_feed() function is not limited to just WordPress sites, but can be used to fetch feeds from any site that publishes them. This function is a part of the SimplePie library incorporated within WordPress, which handles all the RSS/Atom parsing, caching and other functionalities.

Related WordPress Functions