Parsing RSS With WordPress

Sample RSS Feed Screenshot

While fewer and fewer people may be subscribing to RSS feeds (I don’t know if the data actually bear that out yet or not), they are still extremely useful tools in the Web development arsenal. Parsing and using them within a PHP application can be a bit of headache for some, though.

Thankfully, WordPress includes a tool called SimplePie in the core, and it’s really easy to use. It’s very similar to the WordPress HTTP API.

To use SimplePie, the first thing you need to do is to make sure that it’s available to your script. You can do so with some simple conditional code like:

Next, you’ll need to instantiate a new SimplePie object, set the URL of the feed you want to retrieve and parse, set up some other configuration options and start rolling.

The basic instantiation of SimplePie looks something like:

If there’s a problem of some sort retrieving the feed, SimplePie will populate the `$error` property with a message, so you’ll want to check that before moving forward. You can check for errors with some code like:

Once you’ve made sure there aren’t any errors, you can start using the feed data. You can use the `SimplePie::get_items()` method to do so. That function accepts two arguments: a `$start` (the first item you want to start with, based on a zero-index) and a `$length` (the total number of items you want to retrieve).

From there, there are quite a few different methods available through the SimplePie API (the SimplePie Item object) that allow you to retrieve and manipulate various pieces of information for each item. Some of the more common ones are `get_permalink()`, `get_title()`, `get_description()` and `get_enclosure()` (which, once you retrieve an enclosure, has its own methods to get information about that enclosure).

If you put it all together inside of your own class definition, the functions might look something like the following code, which was modified from a custom slideshow plugin I’m developing for UMW.

Comments

  1. Very useful to know, I was pretty sure this was in the core but have not used it yet.

    “the functions might look something like the following code” but there is no code (?).

    Also, the caching data in the database means you fetch that rather than nagging the feed source, right? Is there any cleanup on the database?

    • Hmm. That’s strange. When I view the post (logged in or out), I see the large block of code at the bottom of the post. If it’s still not showing up for you, you should be able to view it on Github at https://gist.github.com/cgrymala/33bbd152ba2c05f859c6#file-gistfile1-php.

      Regarding your question about caching; yes. The way the code is set up, it will cache the feed as a transient in the database for 24 hours (43200 seconds). The way transients work in WordPress, it stays in the database as long as it’s current. That way, every time WordPress tries to use the information, it will pull directly from your WordPress database, instead of checking the original source file.

      Once that transient expires, it will be automatically removed from the database the next time it’s requested, and a new transient will be generated from the original source again. If it’s never requested again, it will stay in the database perpetually.

  2. Now the code shows up, thanks

Leave a Reply