RSS feeds are a very useful animal, but they are rarely used to their full potential. You have tapped into their power if you are one of the few people who uses an RSS reader on your computer or an online RSS reader like Feedly.com. Website owners can take it one step further and publish RSS feed content directly onto their website. You probably want to do that, otherwise why would you read this? So let’s make that happen for your website right now.
If you need more information about what an RSS feed is please check out the video below:
To publish RSS feed information on your website you need two things:
- A plugin that allows you to execute PHP in your posts, pages and widgets. We’re going to use the free Allow PHP Execute Plugin.
- We need a PHP script that will pull in RSS feeds.
Caveat: You can avoid using the plugin if you are comfortable working in WordPress theme files. Just copy and paste the code below into the appropriate file in your theme or child theme. If you’re not comfortable working in those files, read on and install the plugin.
First Things First, Let’s Install The Plugin
Without the plugin the code below won’t work. In the plugin’s settings you will be able to specify where you want PHP code to be executed: any or all of pages, posts and widgets. It’s a security problem to allow PHP to be executed unless you trust everyone who can access the admin area of your website. That is one of the reasons you may want to limit to only posts, or only pages or only widgets.
Once you’ve allowed PHP in one of those three places we can move on to the next step, the script that pulls in the RSS feeds.
Display RSS Feeds PHP Script
This is where the magic happens. Copy and paste the script below into the post, page or widget where you want the RSS feeds to appear.
The code below comes to us courtesy of the WordPress Codex. The cool thing about this script is that you can pull in contents from more than one RSS feed, I’ll explain how below.
get_item_quantity( 10 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
get_description() ); ?>
credit: WordPress Codex
Pull Content From One Or Multiple RSS Feed(s)
Inside the code above there two places where you’ll need to make changes to suit your needs. First, you will need to decide if you want the contents from one RSS or from multiple.
To pull the contents from only one RSS feed use this code by delete the “//” (that’s a PHP comment) before the follow piece of code.
// $rss = fetch_feed( 'RSS_FEED_URL' );
You will also need to replace RSS_FEED_URL with the URL of the actual RSS feed.
If you want to pull content from multiple RSS feeds you will need to remove the “//” from in front of the code I’ve placed below.
// $rsslist = array( 'RSS_FEED_URL1', // 'RSS_FEED_URL2' // ); // $rss = fetch_feed($rsslist);
Remove “//” from all 4 lines and replace RSS_FEED_URL1 and RSS_FEED_URL2 with the actual URLs. If you want add a third RSS feed (or more) just add another comma and another URL like so:
// $rsslist = array( 'RSS_FEED_URL1', // 'RSS_FEED_URL2', // 'RSS_FEED_URL3' // ); // $rss = fetch_feed($rsslist);
How Many Posts?
Next, you will need to decide how many posts you want to pull in total for all RSS feeds in your script. You do this by change the number in this piece of code:
$maxitems = $rss->get_item_quantity( 10 );
The code above will pull, you guessed it, 10 posts. Decide how many posts you want to pull and change the number to that.
Functioning Example
As an example, the full functioning script below pulls 10 financial posts from Forbes’ and Wall Street Journal’s RSS feeds:
get_item_quantity( 10 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
get_description() ); ?>
That’s the end of this tutorial and as always, if you run into any problems let me know in the comments below and I’ll try to help you figure it out.