Building a Recent Post Widget Powered by SimplePie

In this tutorial, I will introduce SimplePie, build a widget plugin that displays recently published articles queried from the WordPress feed using SimplePie, and discuss the performance advantage of querying posts from the WordPress feed instead of the database.

Introduction to SimplePie

SimplePie is a free, very fast and easy-to-use feed parser, written in PHP that handles all of the dirty work when it comes to fetching, caching, parsing, normalizing data structures between formats, handling character encoding translation, and sanitizing the resulting data.

Getting Started With SimplePie

To install and get started using SimplePie, check out the following steps:

In the next section, we will be building a widget that display recently publish articles retrieved from WordPress feed.

Performance Advantage of Querying Post From Feed Over Database

Many of us use a widget to display Recent Posts on the sidebar of our WordPress blog. Many of these plugins query the database to retrieve the recently published articles.

Considering the fact that database-driven web application like WordPress take time querying and writing to the database, it's fair to say that the fewer the number of database queries, the more the greater improvement we can see on our page load time.

Rather than querying the database, we could retrieve the recently published articles from our WordPress feed using SimplePie which can significantly improve performance.

SimplePie has several options for caching feed data built-in so you don’t have to pull the entire feed every time.

One of such is the file-based caching system that store cache feed item in a server-writable directory which further maximize performance.

Building a Recent Post Widget With SimplePie

Building a Widget in WordPress is easy and straight-forward.

First, extend the WP_Widget class, include these four methods: __construct(), widget(), form(), update() in the class and finally register the widget.

Create a class extending the WP_Widget

class Tutsplus_simplepie extends WP_Widget {}

Give the widget a name and description via the __construct() magic method

The WordPress fetch feed function also retrieves feed and parses it using SimplePie. If you wish to use the fetch feed function instead of implementing SimplePie from ground up, change the widget() method inside the Tutsplus_simplepie Widget class to the code below.

Note on the Widget

The appearance of the widget will vary from theme to theme based on your stylesheet. Some of us host our feed on FeedBurner so I added a feed URI option field in the widget settings form incase you want to change it with the default feed set to http://yoursite.com/feed.

Other widget options include altering the number "Recent Post" to display , show / hide post summary and date for each post.

Conclusion

We are done building the recent post widget. To further understand how the widget was built and how to implement it on your WordPress site, download the tutorial file.

When extracted, the file contains two plugin files in zip:

The first is the widget built using SimplePie.

The other built using WordPress fetch feed function which coincidentally also uses the SimplePie library.

Hopefully this helps with working with building widgets and SimplePie in the future.

Agbonghama Collins is a web developer by day and a freelance writer / blogger by night.
He is the developer of the popular ProfilePress Plugin, the best WordPress user account manager.
When not wrangling with code, he is fond of sleeping on the couch and writing on his personal web development blog.
Follow him on Twitter @w3guy.