envato free trialCreative Assets & Unlimited Downloads on Envato Elements Get 7 Days Free

How to Parse RSS Feeds as Content in WordPress

on WordPress

What most people do not know, is that WordPress is also quite capable of pulling in RSS content from outside blogs and parsing it for display within one of its many template files. This functionality is probably not as well known because it is simply not as old and as established as the software’s production of feeds. Included only since version 2.7, the ability to parse RSS feeds as content and include that information into a template file is relatively new and its parameters are largely unknown even among the more advanced members of the WordPress development community at large.

Luckily, these parameters are easy to learn and customize, and they can turn a simple RSS feed into a great source of extra content and discussion on already-busy WordPress blogs.

Not only can RSS feeds be parsed by WordPress, but they can also be split up into columns and tabs. This allows for feeds to actually be used as the primary source of content on some WordPress blogs, especially those that consider themselves a mere aggregator of outside content rather than a producer of unique news, reviews, or commentary on any topic.

Splitting the feeds into columns can be done mostly behind the scenes using an advanced PHP function that works in conjunction with WordPress’ own feed parser. Those who are attempting this feat should be aware that they’ll need pretty extensive PHP knowledge in order to proceed with this task. They’ll also need to understand how the WordPress Loop works, and how each variable that is valid for use within a Loop can be used to display RSS content. Basic XHTML knowledge and some CSS skills would be a great thing to have, as well.

With those skills polished up and ready to be tested, it’s time to go through a few steps and turn a basic RSS feed into a series of columns of aggregated content. Here’s how it’s done:

Step 1: Utilize the Built-in Parsing Function to Pull the Feed in and Turn its XML into Content

The WordPress function for parsing RSS feeds is known as the “fetch” function. This is a pretty apt name, as the function goes out, gets the RSS feed, and brings its content back to the blog. It then turns that XML content into robust, XHTML-encoded database content that can be placed on the page using typical WordPress Loop-style variables. For those who are unfamiliar with this function, the fetch function for an outside RSS feed looks like this example presented below:

include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
$feed = fetch_feed('http://website-of-content.com/content.rss'); // this is the external website's RSS feed URL
if (!is_wp_error($feed)) : $feed->init();
$feed->set_output_encoding('UTF-8'); // this is the encoding parameter, and can be left unchanged in almost every case
$feed->handle_content_type(); // this double-checks the encoding type
$feed->set_cache_duration(21600); // 21,600 seconds is six hours
$limit = $feed->get_item_quantity(18); // fetches the 18 most recent RSS feed stories
$items = $feed->get_items(0, $limit); // this sets the limit and array for parsing the feed

Most of the code above can be left untouched, as there is no customization needed. However, some lines do require alteration. The most important line of code to change is the one that actually pulls the external RSS feed into the database via the site’s feed URL. An incorrect or missing URL will result in all kinds of errors and a distinct lack of content, so be sure to get this information correct.

The second piece of information that will need to be customized is the limit of items that can be parsed from the feed. In the example above, that limit is 18. This will allow the “slice” function later in the tutorial to create three columns of six entries each. All columns will be identical in content and length, and this is a requirement for the routine to work perfectly.

Once those items have been satisfactorily customized, and a feed limit is chosen that will allow for columns of equal content length, it’s time to save the file and then move on to the next part of the tutorial. The next step will ensure that feed items are divided and placed properly throughout the columns of content.

Step 2: Dividing Content into the Right Number of Columns for the Website’s Design

Now that we have “fetched” the feed using a built-in WordPress function that was specifically designed to parse RSS content, it’s time to slice and dice it into columns. Indeed, that’s actually the name of the PHP function that completes this task: php_slice. This is not actually a function that ships with the WordPress. Instead, it’s a raw PHP function that is simply a part of PHP5. Because WordPress itself is a PHP application, and because PHP can be used liberally throughout its standard template files, it’s easy and even recommended to place this PHP function directly into the template file where the feed will be displayed to the site’s readers.

The code that should be pasted in a three-column RSS feed page using WordPress is below. Keen WordPress developers will note that there are three blocks of code, nearly identical in nature, that are essential to getting the job done:

// This block of code creates the first column of six RSS feed items
$blocks = array_slice($items, 0, 6); // Items zero through six will be displayed here
foreach ($blocks as $block) {
echo $block->get_title();
echo $block->get_description();

// This block of code creates the second column of six RSS feed items
$blocks = array_slice($items, 6, 12); // Items seven through twelve are placed in the second column
foreach ($blocks as $block) {
echo $block->get_title();
echo $block->get_description();

// This block of code creates the third column of six RSS feed items
$blocks = array_slice($items, 12, 18); // Feed items thirteen through eighteen go in the third column
foreach ($blocks as $block) {
echo $block->get_title();
echo $block->get_description();

Virtually all of the code presented above can go without any customization or alteration after it has been pasted. There is one line in each block of code, however, that will need to be changed to ensure that the “slices” of content are properly sized and placed within the WordPress template file. Those lines look something like this example, just to use a line from the first block of code:

$blocks = array_slice($items, 0, 6); // Items zero through six will be displayed here

The $blocks variable actually creates blocks of content, thus its pretty straightforward name. In the line above, the variable is responsible for creating blocks zero through six, and placing them in the first column of content. The “zero” block has no content, but it must always be the first number when creating a PHP slice of RSS content. Blocks one through six will contain actual news stories that mirror the feed’s XML content, and they’ll comprise the first column.

The second block of code presents the second set of six stories, and the third block wraps up the process by present the thirteenth through eighteenth stories in the RSS feed. For setups with fewer stories, these numbers would need to be altered so that there is an equal distribution of content in every column. Further chunks of code just like the three above could be created if four, five, six, or even ten columns of content are required on the website. The PHP slice function is pretty easy to alter and bring into any RSS parsing situation, so long as everything remains equal — the number of stories must be evenly divisible by the number of columns, and there must be the same amount of stories in every column created.

Step 3: Creating a Friendly Display of RSS Content Using WordPress Loop Variables

The RSS parsing function creates stories and content that can be displayed and designed using typical WordPress Loop variables. There is just one catch in this case, however: Those variables must have a prefix that references the $block variable. This is so that WordPress knows which content it is styling and which content it is printing on the page. It’s a minor thing to adjust to, however, and it will make sense when looking at the following example of a standard date variable. When placing this variable into a template to display parsed RSS content in columns, it looks like this:

<?php echo $block->get_date("m d Y"); ?>

Pretty easy, right? The same is true of every single WordPress Loop variable when creating an XHTML design to present this feed content to end users. Simply prefix all of the variables with the $block tag and a -> element, and information will print perfectly onto the page. For reference, here’s a pretty basic example of how to bring Loop variables to the table when printing RSS feed content onto the page:

<div class="rss-column-story">
<h3><?php echo $block->get_title(); ?></a></h3>
<?php echo $block->get_date("m d Y"); ?>
<?php echo substr($block->get_description() ?>
<a href="<?php echo $block->get_permalink(); ?>">More...</a>

This is a great way to illustrate just how easy it is to include any information from the feed using a $block variable with a standard Loop variable, combined together. Go ahead and customize this template code so that it matches a website’s current design, and then save the template file itself. There is just one more piece of code that has to be added to the file in order to complete the entire routine and make sure that it is “fail-safe” for end users, even when the remote website is not available.

Step 4: Adding a Layer of Protection Against External Website Failures

One of the main difficulties of pulling content from an external RSS feed is that the website will throw a lot of errors if that external website happens to experience downtime. This is because the “fetch” function will keep trying to fetch stories, over and over, even as the process is relatively hopeless — at least in the short term. This will cause WordPress to output a number of unsightly parsing errors, indicating that the site can’t be reached and that all kinds of bad things are going on behind the scenes. Those errors might be useful for website administrators, but they’re not really the best thing for end users to be confronted with. Indeed, they’ll associate these errors with the website’s integrity and reputation, and they’ll go elsewhere to have their needs met.

Between the “fetch” code and the “slice” code, place the following snippet to prevent such errors from displaying:

if ($limit == 0) {
echo 'News is currently not available due to server issues. Check back soon!';
} else {

This snippet works by placing the slice code into a conditional statement that will only display it if the RSS feed has more than zero stories within it. If the server can’t connect and pull down the RSS feed at all, it will create a zero-story RSS feed. That, in turn, will cause the error message to display, alerting readers to the unavailability of syndicated content. No unsightly PHP errors will display when this code is used.

A Great Way to Enjoy Frequent Content Updates

Pulling down an RSS feed and slicing it into several columns is a great way for less-active blogs to introduce their readers to new content consistently. Thanks to a combination of PHP functions and WordPress Loop variables, these stories can be styled so that they blend perfectly into a website’s design, giving off the impression of a much more active and professional website even when the staff is small and the activity level is low.