Wordpress development techniques #2 – Fetching RSS feeds with wordpress

Fetching RSS feedsAs promised last week, this week we are looking at fetching RSS feeds with Magpie RSS.

Magpie is an PHP based RSS feed parser; I.E. It fetches RSS feeds to display in other places, which gives it many uses.

Unknown to many, wordpress has the magpie RSS parser built in, this article will teach you how to use it, and also how to fetch and show someones del.icio.us bookmarks!

Fetching feeds in wordpress

Wordpress contains two functions for fetching feeds:

  1. fetch_rss -This function fetches and parses an RSS feed.
  2. wp_rss – This function fetches and parses an RSS feed, then displays the results in an unordered list.

These functions use magpie for parsing the RSS, and Snoopy for retrieving the RSS.

Uses the MagpieRSS and RSSCache (http://magpierss.sourceforge.net/) functions for parsing and automatic caching and the Snoopy HTTP client (http://sourceforge.net/projects/snoopy/) for the actual retrieval.


Including Magpie in a template or plug in

Magpie RSS is built into wordpress’ rss-functions.php file (or rss.php in newer versions). It contains the functions needed to fetch feeds, so it needs to first be included:

1
2
// Include the RSS functions. Older versions of wordpress could use include_once (ABSPATH . WPINC . '/rss-functions.php') or include_once (ABSPATH . WPINC . '/rss.php');;
include_once(ABSPATH . WPINC . '/rss-functions.php');

Fetching the feed with fetch_rss

Next lets tell wordpress what feed we want to parse, and put it to work! If you want to get the results and output them yourself, use fetch_rss.

1
2
// Fetch a feed
$feed = fetch_rss("http://yourfeedaddress");

To limit you results you can slice the array of results:

1
$items = array_slice($feed->items, 0, $maxitems);

After that, use a loop to go through the array contents and output the results (see the del.icio.us bookmarks example further down for more detail on this).

Fetching the feed with wp_rss

If you want to output the results in an unordered list right away, use wp_rss.

1
2
// Fetch a feed, replace $limit with the max results you want
wp_rss("http://yourfeedaddress", $limit);

Example output

The below example shows output from my bookmarks feed using wp_rss.

include_once(ABSPATH . WPINC . ‘/rss-functions.php’);
wp_rss(’http://del.icio.us/rss/mikejolley’, 5);

Fetching del.icio.us bookmarks – example

The following code gets my last 5 del.icio.us bookmarks, and outputs them to the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
	// Include the RSS functions of wordpress
	include_once(ABSPATH . WPINC . '/rss-functions.php')
	// Grab my RSS feed
	$feed = fetch_rss("http://del.icio.us/rss/mikejolley");
        // I want 5 results please
	$maxitems = 5;
	$items = array_slice($feed->items, 0, $maxitems);
        // Output the results!
	if(!empty($items)) {
		echo '<ul>';
		foreach ($items as $item) {
			echo '<li>';
			echo '<a href="';
                        // This is a bit messy, but it makes the output valid XHTML strict by removing ampersands
			$item['link'] = str_replace("&", "&amp;", $item['link']);
			$item['link'] = str_replace("&amp;&amp;", "&amp;", $item['link']);
                        // End of messyness. Output the link
			echo $item['link'];
			echo '">';
                        // Output the title
			echo $item['title'];
			echo '</a>';
                        // If i've written a description, output it
			if (isset($item['description'])) {
			        echo '<br />';
				echo $item['description'];
			}
			echo '</li>';
		}
		echo '</ul>';
	}
?>

Neat huh? See what it outputs below:

$feed = fetch_rss(”http://del.icio.us/rss/mikejolley”);
$maxitems = 5;
$items = array_slice($feed->items, 0, $maxitems);
if(!empty($items)) {
echo ‘

That line of code simply sets the output of the fetched feed to be in UTF-8 encoding. Handy.

More about wordpress rss functions


Found this post useful? Why not buy me a coffee!

Related Entries

Popular Entries

12 Responses to “Wordpress development techniques #2 – Fetching RSS feeds with wordpress”

RSS feed for comments on this post.

Pages: « 1 [2] Show All

  • 3 - Jenny says: Reply to this comment

    Gravatar

    I am totally lost. But I wanna figure this out. I really do.

    Comment made on July 16, 2007 at 5:18 pm

  • 4 - Mark says: Reply to this comment

    Gravatar

    Hey, man. I love your site. I use it all the time to make sure I’m coding stuff right. :]

    Comment made on August 3, 2007 at 2:02 am

  • 5 - Lakshmi Mareddy says: Reply to this comment

    Gravatar

    Pretty cool Mike! or should I say “Jolley Good”… I had played with Magpie separately long back, but didnt know it was built-in in WP. Now after reading your article, my mind is racing… A big THANK YOU to you for coming up with this…

    Comment made on September 22, 2007 at 10:52 pm

  • 6 - Christopher Miles says: Reply to this comment

    Gravatar

    Thanks a lot for this post; I’m now using the native RSS fetcher in Wordpress to create a blogroll on my site using del.icio.us links, and a current and recent reading list from my Google Library RSS feed

    I’m spending half my waking day thinking of new and fiendish ways of parsing RSS feeds to my site now!

    (”Half my waking day” and “fiendish” are probably overstating it a bit.)

    Comment made on November 20, 2007 at 2:38 pm

  • 7 - Mark says: Reply to this comment

    Gravatar

    Your lesson is similar to one I found on the wordpress codex but yours is more detailed. I have one question: Do you know if there is a way to display/echo the pubdate for each item? I have not been able to figure that out.

    Thanks!

    Comment made on December 14, 2007 at 11:07 pm

  • 8 - Feedburn your delicious rss feeds! | nemetral says: Reply to this comment

    Gravatar

    [...] a nice how-to on Blue Anvil Journal, but you may see an error happening right in the [...]

    Pingback made on April 23, 2008 at 1:02 pm

  • 9 - Steve says: Reply to this comment

    Gravatar

    Is it just me, or does anyone else see, “Warning: array_slice() [function.array-slice]: The first argument should be an array in /home/sites/blue-anvil.com/public_html/wp-content/plugins/phpexec.php(41) : eval()’d code on line 4″, in the example boxes? The only reason I ask, is because I am getting this error too, when I try to parse a feed from my own website. It works the first time, but then on refresh it dies and give me a similar error.

    Comment made on May 7, 2008 at 4:59 pm

  • 10 - Jamie says: Reply to this comment

    Gravatar

    How can I add things like author and date? I’ve tried things like:

    echo $item['dc:publisher'];

    echo $item['pubdate'];

    Unsuccessfully.

    Comment made on May 12, 2008 at 9:46 pm

  • 11 - Mike Jolley says: Reply to this comment

    Gravatar

    Not sure why there are errors…maybe wordpress team changed something. There is a new function called get_rss however. Maybe that will work.

    Comment made on May 13, 2008 at 11:24 am

  • 12 - bamajr says: Reply to this comment

    Gravatar

    There was an article which caught my attention on MSN about Facebook. (See it by clicking here)

    This article was written by a writer for PCWorld and posted on the PCWorld site. (See it by clicking here)

    This article was pulled by MSN and displayed as a post to their tech site.

    Credit was given to the PCWorld Writer, but it was posted as a new article on MSN.

    This is what I’m trying to do. I want to pull related tech articles from places like PCWorld and put them on my site as a new blog article (Of course, credit would be given to the original writer!)

    How is this done?

    Comment made on May 21, 2009 at 3:40 pm

Pages: « 1 [2] Show All

About this site

Blue Anvil is the online web design journal & portfolio of , a web designer from Norfolk, England. Read More »
ThemeSlice
  • Featured work - More

    • Beefjack
    • Integrity
    • theotaku.com
    • Manic Melon
  • Latest Tweet - More

    • Dropped yoghurt all down my pyjamas. Bugger. Working in my boxers. Ill get dressed some time. Soon.
  • Out of the blue - More

    • Wordpress 2.8 Memory Usage

      With the release of wordpress 2.8 some people are experiencing out of memory php errors along the lines of:

      Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 228968 bytes)

      You may also be seeing pages fail to load fully (for example the wordpress admin dashboard) if php error messages are hidden. I’ve already had one case where we thought certain plugins were to blame but in fact it was just out of memory. A possible fix (which worked in the previously mentioned case) is to add:

      @ini_set('memory_limit','64M');

      to your wp-config file. Has anyone else encoutered this error?

    • jQuery Curvy Corners 1.x and 2.x updated and moved to Google Code

      I have updated the jQuery Curvy Corners plugins (both versions) with jQuery 1.3.2 support and other enhancements. The beta 2 version is looking good and is working in all version of IE, Opera, and Firefox (as far as I can tell).

      You can grab the latest files from Google Code here. Enjoy.

    • I’m too nice: Wordpress Download Monitor plugin page add-on now included with Download Monitor version 3.1.

      It was going to be a paid add-on, but today I had a change of heart and bundled it with the newest version of download monitor. The add-on lets you make a download page using a shortcode; it lists your downloads/categories with full sorting, pagination, and search functionality. Not bad eh? See the documentation topic to see full instructions for usage, or see my download page to see it in action.

      And if you use it, please consider making a donation to ensure the continued development of the plugin!

    • 2 Announcements: New Support forum, and feedback wanted for new download page add-on

      First, I’ve implemented a support forum to Blue Anvil mainly for plugin support and ideas which can be found here. Hopefully this will make supporting my plugins easier. Feel free to add to the discussions (there is also a general web design forum too).

      Secondly, I’ve added a demo of the new download page add-on I’m making for Download Monitor. This will be a paid add-on and it would be cool to get any feedback or suggestions from anyone who would like such a feature. My download page is here. Please leave feedback on the forum or in the comments.