Home » Display, Featured, PHP, RSS

Display RSS Feeds From Another Website Using PHP

28 May 2008 No CommentEmail This Post Email This Post

In this article I will show you how you can add RSS feeds from other websites to your Wordpress website using only a few simple lines of PHP code. No plugin is required.

This ability has got to be both one of the most sought-after Wordpress solutions and one of the least-documented. I will give you the code to implement it and try to explain it as best as I can.

First, in case you weren't aware, the reason that you want to use PHP to display the feed is for Search Engine Optimization purposes. Most solutions you will find on the net for displaying RSS feeds use Javascript. However, Search Engines such as Google can read PHP but they cannot read Javascript.

This means that the RSS feeds you display using PHP are (for most intents and purposes) considered to be content. If that doesn't have you jumping for joy then either you already knew it or you need to go read up on Search Engine Optimization (SEO).

Without further ado, here is the code:

PHP:
  1. <?php
  2.  
  3.           require_once (ABSPATH . WPINC . '/rss-functions.php');
  4.  
  5.           // insert the feed URL here
  6.  
  7.           $rss = @fetch_rss('http://mlb.mlb.com/partnerxml/gen/news/rss/stl.xml');
  8.  
  9.           if ( isset($rss->items) && 0 != count($rss->items) ) {
  10.  
  11.           ?>
  12.  
  13.           <?php
  14.  
  15.           // set the number of items from the feed to display (10)
  16.  
  17.           $rss->items = array_slice($rss->items, 0, 10);
  18.  
  19.           foreach ($rss->items as $item ) {
  20.  
  21.           ?>
  22.  
  23.           <li>
  24.  
  25.           <a href='<?php echo wp_filter_kses($item['link']); ?>' target="_blank">
  26.  
  27.           <?php echo wp_specialchars($item['title']); ?>
  28.  
  29.           </a>
  30.  
  31.           </li>
  32.  
  33.           <?php } ?>
  34.  
  35.           <?php } ?>

The URL 'http://mlb.mlb.com/partnerxml/gen/news/rss/stl.xml' is the RSS feed's address. You can plug any RSS feed URL into this spot.

The line '$rss->items = array_slice($rss->items, 0, 10);' tells the code how many headlines to display. I have chosen 10 but you can choose any other number. (Within reason or your page will never finish loading.)

As with most things I will be writing about here at WordpressIntoCMS.com, you can see this code in action at Metro-East.com, this time on the front page with the Local Headlines and the St. Louis Cardinals headlines.

Keep in mind that the above code will generate headlines only.

If you want to get even more in-depth, there are ways to expand this content.

For example, I have a page which shows all events in the St. Louis area as supplied by the appropriate RSS feed from Eventful. If you visited that link you saw that there is a lot more than just headlines there. (It is worth noting once more that I didn't write any of that. All of the content is generated by Wordpress and this PHP code, courtesy of Eventful.)

Here is the code as it stands as of this writing:

PHP:
  1. <?php
  2. require_once (ABSPATH . WPINC . '/rss-functions.php');
  3.  
  4. $url = 'http://eventful.com/rss/events/?q=&location_type=metro_id&location_id=18&l=St.%20Louis%20metro%20area&sort_order=Mojo';
  5. $rss = fetch_rss( $url );
  6. if ( $rss ) {
  7. $i = 1;
  8. foreach ($rss->items as $item) {
  9. $href = $item['link'];
  10. $title = $item['title'];
  11. $description = $item['description'];
  12. echo "<b><u><a xhref=$href>$title</a></u></b><br />$description";
  13. if ($i == 30 ) break;
  14. $i = $i + 1;
  15. }
  16. echo "";
  17. }
  18. ?>

You can see the URL field again but now I have added other variables to expand the content.

The 'echo' command tells the PHP code what to output. This is where you can style the content. I will be improving this later but as of now you can see that I added some bold font, an underline, and a page break. You can do anything you want here, including CSS.

I hope you'll find this useful as you try to add extra content to your websites using RSS.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • NewsVine
  • Propeller
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • blinkbits
  • BlinkList
  • Furl
  • Ma.gnolia
  • Spurl

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.