Introduction
Fetching RSS feeds in WordPress can be efficiently handled using the fetch_feed()
function. This built-in WordPress function which simplifies the process of retrieving RSS feed items from a remote (WordPress) site.
Step-by-Step Guide
- Setup Your Environment – Ensure your WordPress environment is properly configured. You will need access to your WordPress installation directory.
- Create a Custom PHP File – Create a custom PHP file in your WordPress theme or a custom plugin. This file will include the necessary code to fetch and display the RSS feed.
- Include WordPress Configuration and Functions – To use WordPress functions outside of the default theme files, you need to include the WordPress configuration and functions files. Here’s how you can do it:
Example how to fetch and display RSS
This is a standalone php script that loads wp-config and WordPress. It would be useful if you have an existing PHP site along with the WordPress site.
<?php
/**
* Display the latest 5 posts from a given RSS feed.
* This doesn't load WordPress and is intended to be used from a custom php file and using WP indirectly.
* @link https://wpsandbox.net/1171
*/
define('WP_USE_THEMES', false);
// Adjust the path to wp-config.php as needed.
include_once( dirname(dirname(__DIR__)) . '/wp-config.php' );
include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed('https://example.com/feed/');
if ( ! is_wp_error( $rss ) ) {
$maxitems = $rss->get_item_quantity(5); // specify number of items
$rss_items = $rss->get_items(0, $maxitems);
if ( $maxitems == 0 ) {
echo 'No items found.';
} else {
foreach ( $rss_items as $item ) {
$title = esc_html( $item->get_title() );
$link = $item->get_permalink();
$link_esc = esc_url( $link );
$description = esc_html( $item->get_description() );
echo '<h2><a href="' . $link_esc . '">' . $title . '</a></h2>';
echo '<p>' . $description . '</p>';
}
}
} else {
echo 'An error occurred while fetching the RSS feed.';
}
Running the code above for our site’s fees shows this list
![Image 1](https://wpsandbox.net/wp-content/uploads/2024/06/image-1-1024x626.statopt_ver.1718633031.png)
If you’re using this within WordPress you may want to create a shortcode that outputs the links using the code above.
If you’re accessing a WordPress site you just need to type site/feed to get to the feed source.
The RAW RSS code looks like this
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>WPSandbox</title>
<atom:link href="https://wpsandbox.net/feed" rel="self" type="application/rss+xml" />
<link>https://wpsandbox.net</link>
<description>Free WordPress Sandbox</description>
<lastBuildDate>Mon, 17 Jun 2024 13:56:43 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>
hourly </sy:updatePeriod>
<sy:updateFrequency>
1 </sy:updateFrequency>
<generator>https://wordpress.org/?v=6.5.4</generator>
<item>
<title>How to Fetch RSS Feed/News with WordPress</title>
<link>https://wpsandbox.net/wordpress/how-to-fetch-rss-feed-news-with-wordpress-p1171</link>
<comments>https://wpsandbox.net/wordpress/how-to-fetch-rss-feed-news-with-wordpress-p1171#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Mon, 17 Jun 2024 13:53:38 +0000</pubDate>
<category><![CDATA[WordPress]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1171</guid>
<description><![CDATA[It seems the proper way to fetch RSS feeds is using fetch_feed() WordPress function. Fetching RSS feeds in WordPress can be efficiently handled using the fetch_feed() function. This built-in function simplifies the process of retrieving and displaying RSS feed items on your WordPress site. Below is a step-by-step guide to help you set this up. […]]]></description>
<wfw:commentRss>https://wpsandbox.net/wordpress/how-to-fetch-rss-feed-news-with-wordpress-p1171/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>Why New & Seasoned WordPress Developers Should Avoid Direct php cURL Requests and Use WordPress HTTP API Instead</title>
<link>https://wpsandbox.net/wordpress/why-new-seasoned-wordpress-developers-should-avoid-direct-php-curl-requests-and-use-wordpress-http-api-instead-p1157</link>
<comments>https://wpsandbox.net/wordpress/why-new-seasoned-wordpress-developers-should-avoid-direct-php-curl-requests-and-use-wordpress-http-api-instead-p1157#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Mon, 17 Jun 2024 12:47:17 +0000</pubDate>
<category><![CDATA[WordPress]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1157</guid>
<description><![CDATA[As WordPress continues to gain popularity in the CMS market, it attracts developers from various backgrounds, including those with strong PHP skills. However, some PHP habits, such as directly using cURL for making HTTP requests, can lead to less maintainable and less secure code. WordPress offers a powerful alternative with its built-in HTTP API, which […]]]></description>
<wfw:commentRss>https://wpsandbox.net/wordpress/why-new-seasoned-wordpress-developers-should-avoid-direct-php-curl-requests-and-use-wordpress-http-api-instead-p1157/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Fix Visual Studio Code Crashes on Ubuntu</title>
<link>https://wpsandbox.net/development/fix-visual-studio-code-crashes-ubuntu-p1139</link>
<comments>https://wpsandbox.net/development/fix-visual-studio-code-crashes-ubuntu-p1139#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Sun, 16 Jun 2024 14:42:11 +0000</pubDate>
<category><![CDATA[Development]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1139</guid>
<description><![CDATA[As a developer, dealing with an IDE that crashes on startup is incredibly frustrating. Visual Studio Code (VS Code) is a popular choice, so when it fails to work, productivity can take a hit. Recently, I encountered this issue on Ubuntu, and here’s how I resolved it. The Problem When launching Visual Studio Code, it […]]]></description>
<wfw:commentRss>https://wpsandbox.net/development/fix-visual-studio-code-crashes-ubuntu-p1139/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Disable Hardware Acceleration in Visual Studio Code</title>
<link>https://wpsandbox.net/development/how-to-disable-hardware-acceleration-in-visual-studio-code-p1131</link>
<comments>https://wpsandbox.net/development/how-to-disable-hardware-acceleration-in-visual-studio-code-p1131#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Sat, 15 Jun 2024 20:47:32 +0000</pubDate>
<category><![CDATA[Development]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1131</guid>
<description><![CDATA[For some reason you may need to disable Visual Studio Code’s hardware acceleration. For example in Ubuntu it’s slow. !Backup! Before you do anything backup the settings. CTRL+SHIFT+P and type runtime you should see this kind of option. https://code.visualstudio.com/updates/v1_40 … or you can edit $HOME/.vscode/argv.json file. (Linux) that line is commented. When I uncommented it […]]]></description>
<wfw:commentRss>https://wpsandbox.net/development/how-to-disable-hardware-acceleration-in-visual-studio-code-p1131/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Preserve WP-CLI Table Formatting When Redirecting Output</title>
<link>https://wpsandbox.net/wp-cli/how-to-preserve-wp-cli-table-formatting-when-redirecting-output-p1122</link>
<comments>https://wpsandbox.net/wp-cli/how-to-preserve-wp-cli-table-formatting-when-redirecting-output-p1122#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Sat, 15 Jun 2024 10:11:03 +0000</pubDate>
<category><![CDATA[WP-CLI]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1122</guid>
<description><![CDATA[WP-CLI is a powerful command-line tool for managing your WordPress sites. It provides various commands to interact with WordPress, similar to how MySQL CLI allows you to manage MySQL databases. One of the useful features of WP-CLI is its ability to display results in a neatly formatted ASCII table, making it easy to read and […]]]></description>
<wfw:commentRss>https://wpsandbox.net/wp-cli/how-to-preserve-wp-cli-table-formatting-when-redirecting-output-p1122/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Enable Debugging Mode in WordPress</title>
<link>https://wpsandbox.net/wordpress/how-to-enable-debugging-mode-in-wordpress-p1026</link>
<comments>https://wpsandbox.net/wordpress/how-to-enable-debugging-mode-in-wordpress-p1026#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Mon, 10 Jun 2024 08:03:11 +0000</pubDate>
<category><![CDATA[WordPress]]></category>
<category><![CDATA[debug]]></category>
<category><![CDATA[debugging]]></category>
<category><![CDATA[troubleshooting]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1026</guid>
<description><![CDATA[Download the article as PDF Debugging is an essential process in any software to figure out what and when it went wrong. WordPress has a way to enable debugging. It is normally turned off by default because it’s not very professional to show errors on the site and this causes users to trust less that […]]]></description>
<wfw:commentRss>https://wpsandbox.net/wordpress/how-to-enable-debugging-mode-in-wordpress-p1026/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Fix Docker Error: docker: ‘compose’ is not a docker command</title>
<link>https://wpsandbox.net/devops/how-to-fix-docker-error-docker-compose-is-not-a-docker-command-p1022</link>
<comments>https://wpsandbox.net/devops/how-to-fix-docker-error-docker-compose-is-not-a-docker-command-p1022#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Wed, 29 May 2024 15:00:23 +0000</pubDate>
<category><![CDATA[DevOps]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1022</guid>
<description><![CDATA[You’re an awesome WordPress developer and wanted to use docker with WordPress and run into an error. in docker v1 to use docker compose you had to use docker-compose … but in v2 docker compose is added a docker subcommand i.e. docker compose To install Docker Compose v2 run this command as root.]]></description>
<wfw:commentRss>https://wpsandbox.net/devops/how-to-fix-docker-error-docker-compose-is-not-a-docker-command-p1022/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>How to Fix docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument ‘chunked’</title>
<link>https://wpsandbox.net/devops/fix-dockerexception-error-while-fetching-server-api-version-httpconnection-request-got-an-unexpected-keyword-argument-chunked-p1016</link>
<comments>https://wpsandbox.net/devops/fix-dockerexception-error-while-fetching-server-api-version-httpconnection-request-got-an-unexpected-keyword-argument-chunked-p1016#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Wed, 29 May 2024 14:48:30 +0000</pubDate>
<category><![CDATA[DevOps]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=1016</guid>
<description><![CDATA[You will most likely see this error even on a fresh Ubuntu installation. It’s really strange how nobody caught that. Yes, it’s frustrating, I know especially if you’re very busy. it is possible to that this error is related to some python modules/libraries  requests<2.29.0′ and ‘urllib3<2.0 according to the ticket below. They suggest downgrading but […]]]></description>
<wfw:commentRss>https://wpsandbox.net/devops/fix-dockerexception-error-while-fetching-server-api-version-httpconnection-request-got-an-unexpected-keyword-argument-chunked-p1016/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>PHP Warning: file_get_contents failed to open stream: Connection refused in script.php</title>
<link>https://wpsandbox.net/php/php-warning-file_get_contents-failed-to-open-stream-connection-refused-in-script-php-p998</link>
<comments>https://wpsandbox.net/php/php-warning-file_get_contents-failed-to-open-stream-connection-refused-in-script-php-p998#respond</comments>
<dc:creator><![CDATA[WPSandbox]]></dc:creator>
<pubDate>Tue, 30 Apr 2024 15:34:19 +0000</pubDate>
<category><![CDATA[PHP]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=998</guid>
<description><![CDATA[You can create a temp file in your document root folder and after the tests you will need to delete it so you don’t expose extra sever info to always curious spammers/hackers. if you get this php error you may need to check your php.ini settings to ensure that the allow_url_fopen ini setting in the […]]]></description>
<wfw:commentRss>https://wpsandbox.net/php/php-warning-file_get_contents-failed-to-open-stream-connection-refused-in-script-php-p998/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>What is a Document Root folder?</title>
<link>https://wpsandbox.net/development/what-is-a-document-root-folder-p999</link>
<comments>https://wpsandbox.net/development/what-is-a-document-root-folder-p999#respond</comments>
<dc:creator><![CDATA[superadmin]]></dc:creator>
<pubDate>Tue, 30 Apr 2024 15:33:20 +0000</pubDate>
<category><![CDATA[Development]]></category>
<guid isPermaLink="false">https://wpsandbox.net/?p=999</guid>
<description><![CDATA[The document root folder is the root folder of your site. For Ubuntu servers it’s in /var/www/html and is owned by www-data user. For other sites that are hosted on the server it’s usually one of these: The document root folder may or may not necessarily be the one where WordPress is installed because it […]]]></description>
<wfw:commentRss>https://wpsandbox.net/development/what-is-a-document-root-folder-p999/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
</channel>
</rss>
Common RSS errors
Deprecated: File rss.php is deprecated since version 3.0.0! Use wp-includes/class-simplepie.php
It seems the whole wp-includes/rss.php was totally deprecated for some (good) reason.