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:

  1. Download SimplePie library and create two folders: php and cache.
  2. Upload library/ and autoloader.php to the php folder you just created.
  3. SimplePie is now installed and ready to be use.

Let's see how to use SimplePie to retrieve content found in a feed.

SimplePie is easy to install and use, isn't it?

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.

Recent Post Widget powered by SimplePie
  1. Create a class extending the WP_Widget
  2. Give the widget a name and description via the __construct() magic method
  3. The widget() method displays the front-end of the widget.
  4. Create the widget settings form with the form() method
  5. Finally the update() method sanitizes and saved the widget settings to the database.

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

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

Recent Post widget setings form


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:

  1. The first is the widget built using SimplePie.
  2. 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. 

Let me know your thoughts in the comments!



Related Articles