Getting Started with the WordPress Transient API, Part 2

In the first post in this series, we took defined what the API is, how it differs form the settings API, and some of the general calls that we can make to the API. In this post, we'll be taking a look at a practical implementation of the API and how to handle some idiosyncrasies that come with dealing with expired data.

The WordPress Transients API is a powerful (but really-easy-to-use) aspect of the WordPress API. Generally speaking, it makes it really easy to store data with an expiration time, and makes it really easy to take advantage of various caching plugins to ultimately increase the speed of your site.


Setup The Plugin

For the purposes of this plugin, we're going to create a simple widget that will list a blogs top commenters of all time. The goal of the plugin is to keep it lean so that we can highlight the transients functionality of the plugin.

Note that all of the plugin's files can be retrieved from GitHub at any time. In the mean time, go ahead and create a directory called 'top-commenters-cached' and make sure that it has the following directory structure:

If you're not interested in localizing the plugin, feel free to leave the 'lang' directory out of the plugin. At this point, we're ready to begin writing the widget.


Basic Functionality

The plugin is simple. It should…

  • Allow the user to give the widget a custom
  • Retrieve the top 10 most popular commenters of the life of the blog

Easy enough. Here's the code for the basic plugin. Note that it's commented throughout so spend sometime reading through it to understand what we're doing. If you're unfamiliar with the widget API, don't forget to check out our WordPress Widget Boilerplate post.

Next, let's take a look at the widget's view. This is the part of the plugin that's responsible for displaying the list of comments. It works by displaying the widget's title (if it's defined), then loops through the results creating a new list item.

Obviously, we've left out part of the code. Namely, the admin panel. It should simply allow for users to enter a title for their widget:

Remember that you can view the full source code and download the plugin from its GitHub repository.


Cache The Data

At this point, we have a functional plugin; however, we're not actually caching any data yet. The most intensive part of this plugin is when we're querying the database and the results of the query are what we actually want to cache so let's do that.

Locate the query in the code:

And let's store the results for 12 hours using the transients API:

Pretty easy, right? Of course, we're not done yet.


Retrieve The Data

Once the transient is set, we need to be able to retrieve the transient. Let's set that up now:

That's all there is to it!

But wait - if you recall from the first post in the series, transients actually expire so we're not guaranteed to retrieve the transient.


Finding Missing Data

Regardless of what you're doing, retrieving data that has expired generally follows the same process:

  • Check for the existence of the transient
  • If it exists, use it
  • If it doesn't exist, set it then retrieve it

So let's do that within the context of our plugin:


Conclusion

Not too bad, right?

As you can see, working with the Transients API requires little more than knowing when to use it and what functions are available. In my opinion, it's one of the most powerful aspects of the WordPress API.

If you find yourself retrieving large amounts of data, looking for a way to expire data for a refresh, or simply wanting to take advantage of caching plugins, remember to take advantage of the Transients API.

Tags:

Comments

Related Articles