Caching in WordPress using Transients API can store the data which have an expiration time. After the data has expired or not available, we can run code to grab data and catch it again. Transients API in WordPress makes caching process very easier to store a data for limited time.
Once in a client project in WordPress, I had need to obtain some data from remote server, process it and display whenever a request is made for the data. Calling remote server along with processing, were too expensive operations. So it was needed to cache data for few hours and request only when it would expire.
For the caching purpose, I checked WordPress documentation and found that using Transients API, I can easily cache data. Further I hadn’t any need to keep track of it’s expiration. Just an if
condition and know if data still exists.
Interested in WordPress related practical stuff? Click here.
Here in this article I will tell you how to use WordPress Transients API to catch data for hours / month or years. You can speed up your site’s performance by catching some sort of data and by escaping the need to generate with each request. This type of catching we’re implementing is different from plugins like W3 Total Cache which cache whole page for speed up page rendering.
Caching in WordPress – Transients API
To cache the data, first we need to understand how does Transients API work and how to safely use it. The API is similar to WordPress Options API plus a expiration time feature added.
Transients can be stored either in wp_options
database table or in fast memory of server if memcache or any caching technology is installed there. So transients should also never be assumed to be in the database, since they may not be stored there at all.
Convert your simple WordPress dropdown menu to Mega Menu.
For a permanent storage, database is obvious choice. Caching with transients should be used to store any data that is expected to expire, or which can expire at any time.
//Save a transient set_transient() //Get a saved transient get_transient() //Remove transient if manually removal is necessary delete_transient() //Work network wide when using WordPress Multisite set_site_transient() get_site_transient() delete_site_transient()
Likewise general caching concept, it is possible for the transient to not be available before the expiration time. So code must check for existence & should re-generate the data and cache in transient for next period.
Code to Apply Data Caching in WordPress
<?php $transient = 'unique_name_of_transient'; if ( false === ( $data = get_transient( $transient ) ) ) { //How longer you wish to cache data, 6 hours here $duration = 6 * 60 * MINUTE_IN_SECONDS; //Write code to re-generate the data $data = code_to_regenerate_data(); set_transient( $transient, $data, $duration); } return $transient; //================================ //Delete $transient delete_transient( $transient); ?>
The code above is a simplified example to safely cache data. You can store data using transient like shown the example code. We first checked for data existence, generated if it’s not & store again in cache and finally returned the data. `MINUTE_IN_SECONDS` is Time Constants introduced in WordPress. You’re free to use hardcore value of seconds for caching duration.
Caching in WordPress using Transients API is a powerful feature. You can use it to:
- Reduce number of database call
- Cache data retrieved from third party API or using curl
- Store query results to boost site performance
- Static page caching
Read more about WordPress Transients API or share your thought in comments about data caching in WordPress.