Parsing RSS With WordPress

VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

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:

See the gist on github.

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:

See the gist on github.

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:

See the gist on github.

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.

See the gist on github.

VN:F [1.9.22_1171]
please wait...
Rating: 0.0/5 (0 votes cast)
Exit mobile version