What Are WordPress Transients and How to Manage Them
Everyone wants your website to run a bit faster. You want it because you’re losing visitors to slow loading times. The visitors want it because otherwise, they’ll have to do another search and they just did one. Search engines want it because they’re into delivering the best possible experience to the searchers, so your sluggish website is cramping their style, too.
One of the ways to speed up a website is to cache some data in a handy place where it can easily be accessed thus dampening the impact of these requests on your server. You can do it with caching plugins, but before you do, it might not hurt to learn a thing or two about WordPress transients.
In this article, we’ll explain:
Transients are types of cached data enabled in WordPress by the Transients API. You can use the transient to store data, but only for a while – a transient will have an expiration date after which you won’t be able to access the data. So, transients are a type of temporary data cache.
A transient will consist of three parts:
Its name or key ($transient) – a string used to identify the transient.
The information ($value) – an object, array, number, or a string that’s the information retrieved by the API.
Expiration ($expiration) – the maximum period for which the transient is valid to stay in the database.
Transients might seem similar to cookies, but they’re different in several ways. Not only that they’re stored in the database and not the visitors’ browsers, but transients are much more powerful than cookies.
Transients are also tricky to handle in a couple of ways. For one, they are routinely stored in fast memory instead of the database by caching plugins, making them more difficult to locate. Next, they can expire at any time – the expiration date you set really is the maximum time they’ll be valid for. Finally, even when they expire, transients are not deleted – they are either deleted when someone tries to access the information in an expired transient, or by you manually.
The most common use case for transients is resource-intensive queries. For example, multiple users asking the same database-intensive query at the same time can put a serious dent in the website’s performance. But with a transient, the data they’re looking for can be cached temporarily, and the whole expensive process can only be instigated if someone tries to retrieve data from a transient that expired.
Transients have found a use in other areas, too. They’re commonly used for sidebar widgets that display the number of visitors, tweets, or a photostream. They’re very useful when you want to get data from an external source such as Twitter or Facebook. Whenever you’re operating with data you’re fine with disappearing at any time, probably because you have a way to fetch it again, transients are a good choice. For permanent data storage, use Options.
You can do three things to transients. First, you must save the transient. Usually, it will go to the database, but remember, it might end up in the fast memory. To save a transient, you should use the following syntax:
set_transient( $transient, $value, $expiration );
Just remember to replace the appropriate values with real ones.
Next, you can fetch a transient you’ve saved using the following syntax:
get_transient ( $transient );
You’ll only need the transient key for this, and you’ll only get an answer if the transient hasn’t expired yet. If it has, you will get a false value.
Finally, you can delete the transient even before it has expired. To do so, you’ll have to use the syntax:
delete_transient ( $transient );
This will remove the transient. As you might have noticed, transient names are very important, so make sure you have a naming system in place.
If you want a more convenient way to manage your transients, you can download and install the Transients Manager plugin.
After activating it, you can head over to Tools > Transients, and there you can find a full list of transients, including their names, values, and expiration dates.
The plugin will give you several options for deleting transients in bulk – you can delete all of them, the ones that expired, or the ones with an expiration date. Keep in mind that it’s always a good idea to delete expired transients. You can also delete the transients separately, and you can edit their names, values, and expiration dates, too.
Let’s Wrap It Up
In the never-ending quest to make your website faster, you can use every ally you can find. Transients are among the better ones, even though they’re sometimes overlooked. But the more you learn about them, the things they can do and the tricks you can pull off with them, you’ll get more and more interested in using them and they’ll become more and more useful.