Embedding RSS Anywhere in WordPress

14 March 2008 by TQuizzle, 7 Comments

For some reason or another, I never figured out until today digging through the widgets files in wp-includes directory in your default WordPress installation, that you could easily insert RSS feeds anywhere in your site.

This started with a task I had for MakeUseOf.com to include RSS feeds for the author pages. After digging into it, I found it was quite easy and it uses the Magpierss code almost exactly.

All you really have to do is insert a little code in the template file you want to use (or a page or post using one of many php inclusion plugins for WordPress).

Insert this into the top of the file, post or page as it sets up the RSS parser:

<?php  /* WP RSS Reader Includes */
    require_once(ABSPATH . WPINC . '/rss.php');
?>

Then to include the feed:

<?php
   $num_items = 10; 
   $url = http://feed_url_you_want;
   $rss = @fetch_rss( $url );
   $items = array_slice($rss->items, 0, $num_items);
  echo "<br />n<h4>" . $rss->channel['title'] . "</h4>";
   echo "<dl class="thepost">n";
   foreach ($items as $item) {
       $href = $item['link'];
       $title = $item['title'];
       echo "<dt><a href=$href>$title</a></dt>n";
   }
   echo "</dl>";       
   ?>

Here I use the "array_slice" to only grab a set number of items and  a definition list to display them, you could just as easily substitute this to use an unordered list. I also only grab the headlines. You could just include the description of the feed by adding the variable $desc = $item['description']; in the foreach loop.

If you’d like to be nice to your webserver, setup a magpierss cache directory. Create a directory on your server that has chmod 777 privileges and define it after you setup the RSS parser.
Simply add this:

define('MAGPIE_CACHE_DIR', ABSPATH . '/path_to_your_cache_directory');

There you go, you now can easily pump feeds inside anywhere in WordPress.

Do you like?

Related posts:

7 Responses to “Embedding RSS Anywhere in WordPress”

  1. Kitkat 27 March 2008 at 3:33 am #

    Good tip. Now I can go and whack some RSS feeds in my blog… make it look more lively. :)

    Anyway, you think you will be interested to write some articles for my blog?

    Cheers.

  2. Sumesh 27 March 2008 at 4:24 pm #

    Hi Travis,
    Any progress in finding how to insert that linked.php I told you about? The feed file is /wp-includes/feed-rss2.php
    It is the placement that I am not sure about. Although I’ve installed WP on my PC, things aren’t working out as expected. Any help with the correct place to insert the linked.php code would be greatly appreciated.

    Also, does feed-rss2 support the various tags and custom field calls (that I have used in linked.php)?

  3. TQuizzle 3 April 2008 at 8:59 am #

    @Sumesh
    Try looking into the FeedFooter plugin. It basically does exactly what you’re looking for (I think) and if you were to delve into it, you might find where it adds itself to the feed.

  4. Adam 12 February 2010 at 9:45 am #

    I am placing the code provided in this post into my sidebar.php file. However, whenever I do so, my website just comes back blank. Any idea?


Leave a Reply