Maintaining a cache can allow a website to load quicker and more efficiently than pulling and generating data during each page load. For most data, WordPress has a group of functions dedicated to storing data that will stay relevant for a specific period of time. WordPress also allows plugins to change the location of transient data, say to a memcached location, in order to speed up the cache even more. For this reason, it is easy and effective for plugin and theme authors to create a quick and easy cache solution without having to recreate a cache system for every piece of software.
What is Transient Data?
Put simply, transient data is information that is temporarily stored for a set time, after which it expires and needs to be renewed or recreated. It is meant to as storage for information that you will need again in the future, but has the possibility of becoming stale or incorrect.
By default, WordPress stores transient data in the options table of that installation’s MySQL database. As stated before, it is possible for a plugin to move the transient data away from the database, so creating your own MySQL queries for obtaining transient data set by WordPress’ functions would not be advisable. It is also important that no stable or semi-permanent data is stored as a transient, for these may be deleted upon a server reboot or a clearing of all transient data.
There are many possible uses of transient data in any theme or plugin. One such possibility is for displaying the number of people following a social media account. It may be useful to grab the number of followers, store that information for use during a specific time period, and use the stored data for each page load. In the case that a social media website is experiencing issues, a website that has cached the follower data will not suffer the effects for some time.
Another a possible use of transient data is storing information about when an action was last completed. Say you wanted your software to run a particular function only when the last run was more than a specific time ago. For example, a function that sends an email to someone when an update is available may not the recipient’s inbox filled in case many updates become available within a short span of time. Your software could create transient data what would expire after your specified buffer time, and first check to see if that data exists when the function runs. Transient data is better suited than a cron entry for this example due to the unpredictable timing of when updates become available.
Something that you may not know about transient data is that WordPress allows you to store not only simple strings of data, but allows you to store and retrieve arrays and objects as well. This means that your software does not need to adapt its output, reducing the load on your and your software. If your software had an array to store, it could later retrieve that exact array at a later time or on a different page load. Even if you had created an object to store and process information, you can use transient data as a cache for information that will likely remain consistent for some period of time.
Yet another aspect that may be useful is certain situations is that you can set transient data to never expire by setting its expiration time to zero. This will also tell WordPress to automatically load the data on every page load. This may be advantageous over using the Options API for situations where your software needs a cache to store information that your software will refresh on a timely basis. You can set a cron job to refresh the information every so often, ensuring that the cached data is up to date but never deleted automatically.
Using the Transients API
The Transients API has three main functions that relate to setting, getting, and removing transient data.
To work with the entire network in a WordPress Mulitsite environment, each of those functions have a related function that is essentially the same, requiring only the normal inputs. The only difference is that “site_” is added before the word “transient” in the function’s name, so set_transient() would become set_site_transient() in order to handle information relevant to the entire network.
Setting Transient Data
The only function set of the Transients API that takes more than one argument, set_transient() is what your software will use to add or modify transient data. Its three arguments are the transient data’s name, the value to be stored, and the amount of second until the data should expire.
set_transient() in your software, modify the code below for your own use:
The first argument is what is used to identify your data in the transient system. When stored in the database, your transient data is stored either as “_transient_” or “_site_transient_” followed by the name you assigned to your transient data. Due to some restrictions, this name should be less than 45 character for a normal transient and less than 40 characters for a site transient. The second argument is the string, array, or object that you need stored. The final argument is how many seconds you want the data to remain active. After this amount of time, the data will be removed when WordPress is next loaded. If you choose, as stated in the previous section, you can set this argument to zero in order to never let the data automatically expire and automatically load the data during every visit.
As of WordPress 3.5, five time constants were added to ease the setting of transient data. You can read more about the five and how to use them within the Transients API page in the WordPress Codex. They reduce the amount of math you or your software needs to accomplish, but they do not need to be used if unneeded.
Retrieving Transient Data
When you want your software to get transient data that you have previously stored, get_transient() is the function that you will call. The only argument that it accepts it the name you assigned to the transient data in
set_transient(). In return, it will give either your data if it was previously set and has not expired, or false if the transient was never set or has expired.
Deleting Transient Data
If you had previously set transient data to never expire, or want to clear out everything when your software is deactivated or removed, you can forcefully delete any transient you have before the expiration time using delete_transient(). If you do want to delete your transient data upon theme or plugin deactivation, you can call
delete_transient() in a plugin’s uninstall.php file or within a function similar to the example below for a theme.
Putting It Together
An example of how to retrieve transient data, or set it if it’s not available, can be found in the code below. It is safe to store Boolean data as transient data using the example since the only time the result of
get_transient() will be identical to false is when WordPress fails to find the transient. The following example also checks to see if the returned data is empty, meaning that the transient exists but contains nothing.
Transient data allows plugins and themes to utilize a simplistic caching system for many types of data. The ability to cache local or remote data for later use allows a website to reduce the amount of processing needed during each load, and therefore taking less time for each page to load. By using the Transients API to store temporary information, a plugin or theme can help to reduce the time and resources of a page load, and help to reduce the amount and severity of errors caused by other services going offline.
Link: Transients API in the WordPress Codex