Optimizing Your WordPress Theme With The Transient API
The Transient API is a underused API within WordPress that allows you to store data for a limited amount of time. The data is then destroyed and can then be refreshed. Great for caching or storing data temporarily.
What is the Transient API?
The Transient API is extremely similar to the options functions that are commonly used to store theme and plugin options. It also can be used to store other data, however the main difference is that the data is set to expire after a certain period of time.
For reference the three main transient functions are get_transient($slug)
, set_transient($slug, $data, $expiry)
and delete_transient($slug)
.
How Can It Improve My Theme?
The Transient API can be used to improve your theme by caching information that is dynamic, but can stay cached for a small period of time. Since the Transient API has an expiry you can use it to store dynamic information for a small period of time, then have it delete itself and refresh the data. This will take the stress away from WordPress generating the data via a query.
Please remember though that WordPress has it’s own object caching system so it is best to reserve using transients for queries that are extra stressful for WordPress to generate. An example of this would be grabbing a few posts from several different categories since it normally requires a single query for each category, or queries that require large amount of joins. This is especially true for underpowered MySQL servers such as those used on shared hosting accounts.
Can I See An Example?
Sure. Let’s make a simple piece of code that does a query, then stores it in a transient set to expire within 12 hours. This isn’t very useful since the query is super simple, but the same code works for pretty much any query.
1 2 3 4 5 6 7 |
$posts = get_transient('my_super_posts'); if(!$posts) { $posts = get_posts(array('numberposts' => 10, 'post_type' => 'post')); if($posts) { set_transient('my_super_posts', $posts, 60*60*12); } } |
Now hopefully the code looks simple because it is fairly simple. First we ask for data from our transient, just in case we’ve already stored the posts. If it returns false we use get_posts()
to grab some posts & store them in the transient. WordPress takes care of serialization of arrays/objects automatically so no need to do that. Since the transient function (set_transient()
) takes the last parameter as seconds we use a quick sum to get the number of seconds in 12 hours.
It really is that simple. The only limit to using transients is your imagination. I’ve come up with a simple rule: If you need to store data & it needs to expire then transients are the way to go. Any other data, especially if it doesn’t need to expire you’ll most likely want to store using the WordPress options functions instead.
Another consideration for the transient would be to cache data from a social media API such as Twitter. Since you need to cache the data to avoid exhausting your allocated API requests the transient API is ideal for storing the data since you can easily detect it’s expiry and repopulate it with new data.
Conclusion
I’ve recently found myself using the Transient API more and more when working in WordPress for clients. I recently used it to create a multi category query as I mentioned in this tutorial. Without it the server was put under a lot of strain, now it only has to cope with that strain once ever few hours when the cache runs out. Yes, if a new post is created there is a delay in it’s display, but I (and in this case the client) agree that it is preferable to a slow server.
1 Comment
Gordon Freeman
An interesting article, thanks Paul! Is this used for and/or safe to be used for security, such as cached user credentials and the like?
Or what about storing data from intermittent off-site API requests such as Twitter, Facebook and RSS feeds?