Adding External Content (via RSS) to WordPress

This post has been marked as old. The code might no longer work. Comments have been disabled as this is no longer maintained. Use the search on the right to find a newer tutorial.

One of my current clients (Project7) wanted a feed of Global News in their sidebar that pulled news articles from many different sources. RSS is perfect for this, but there’s a problem with most of the RSS plugins out there – they will pull the news articles into your site as blog posts. While this might not be a problem for some projects, when you’re dealing with hundreds of news articles a day you’ll need a solution that doesn’t store them locally.

First I’m going to cover how to use SimplePie (which already comes with WordPress) to pull in an RSS feed. Then, I’ll briefly cover how we used Yahoo Pipes to create a single RSS feed from many.

Looping through an RSS Feed

You’ll want to place this code where you want the RSS feed to appear. In my case, this is the sidebar.php file.


<?php
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('http://pipes.yahoo.com/pipes/pipe.run?_id=057832c1dcec7b587df982d689373923&_render=rss');
if(!empty($rss)):
$maxitems = $rss->get_item_quantity(5);
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>

Here’s what it’s doing:

  • include_once(ABSPATH . WPINC . '/feed.php'); – Pull in the the feed.php file which contains SimplePie.
  • $rss = fetch_feed('http://pipes.yahoo.com/pipes/pipe.run?_id=057832c1dcec7b587df982d689373923&_render=rss'); – Fetch your feed. Set this to whatever RSS feed you want (as I mentioned, we’re using a Yahoo Pipe).
  • if(!empty($rss)): – If the RSS feed isn’t empty …
  • $maxitems = $rss->get_item_quantity(5); $rss_items = $rss->get_items(0, $maxitems); endif; – Set the limit to the 5 most recent items (you can set this to whatever you want).

That pulls in your RSS feed; now it’s time to do something with it. We’ll run it through a loop that displays the title (linked to the original permalink) and the summary. For a full list of what you can display, check the SimplePie Documentation.

First, the code:


<ul>
<?php if ($maxitems == 0) echo '<li>No news.</li>';
else
foreach ( $rss_items as $item ) : ?>
<li><strong><a href="<?php echo $item->get_permalink(); ?>" rel="bookmark"><?php echo $item->get_title(); ?></a></strong> - <?php echo $item->get_description() ?> </li>
<?php endforeach; ?>
</ul>

Here’s what it’s doing:

  • <ul> – Start an unordered list
  • <?php if ($maxitems == 0) echo '<li>No news.</li>'; else foreach ( $rss_items as $item ) : ?> – If there’s nothing in the RSS feed, display “No News.” If there is posts in the RSS feed, run through them individually, assigning them to the variable $item.
  • <li><strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong> - <?php echo $item->get_description() ?></li> – The <li> defines it as an item of the unordered list, <strong> bolds it, we’re then linking the title of the post to the permalink (the original source of the content), closing the link and the <strong>, then displaying the summary of the post and ending the list item.
  • <?php endforeach; ?> </ul> – The endforeach ends the loop. When we’ve gone through all the post items, close the unordered list.

That’s pretty much it. The one on Project7’s site is a bit more complicated (we had to analyze the permalink to determine what kind of icon to display next to the post),  but that’s how you display posts from an external RSS feed on your site, without storing those posts in your WordPress database.

Finally, I wanted to touch on Yahoo Pipes. If you haven’t used it before, it’s a great visual way for doing common programming mashups. Since we have 6 RSS feeds that we want included in this, and we want the 5 most recent posts from the total, I used Yahoo Pipes to combine these feeds into a single one. Below is a screenshot of my Pipe, along with a walkthrough of what it’s doing:

pipes

  • First I created 6 “Fetch Feed” objects, and added the URL to the individual feeds to them.
  • I then used the “Union” tool to group them together (you can only Union 5 objects, so I had to do two Unions with 3 objects each, then a Union on those two Unions)
  • I then piped it through a “Sort” filter that sorts all the posts in descending order.
  • I then limited the feed to 5 posts. While this might not be necessary for you since we were limiting the feed length above in WordPress ( $maxitems = $rss->get_item_quantity(5); ), the final feed I pulled in had hundreds of posts and overloaded WordPress.
  • I then sent this to the Pipe output.

Once you’ve saved the Pipe, you can go to Run Pipe, which will display the output. From there you have the option to “Get as RSS” – take that URL and paste it in the above code.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Ryan says

    So is all of this code in the same php file? Not sure if the first group of code is separate from the second?

  2. Chris Bratlien says

    Several extra things I’ve learned about this technique: The Fetch Feed object within Yahoo Pipes lets you add multiple feeds, which obsoletes having to use the Union object. Also, today I learned that it’s important to use the Truncate object as the last object in the pipe. Today I had a feed with 337 items and that was crashing WordPress. Once I truncated down to only the number I needed (15 in this case), WordPress was much happier.

    • Bill Erickson says

      Ah this must be a new feature because when I made this post more than a year ago it seemed like the Fetch Feed only allowed one feed at a time.

      Re: truncating. That’s what my second to last bullet addressed 🙂