Nowadays Caching is very important for any website development and WordPress provides own API for caching a data which is known as Transients API.Transients storage API is useful when you are using complex queries in your themes and plugins in WordPress. So Today we’ll talk about how you can use Transients APIs in WordPress itself.
What is Transients API?
Web page data caching is a way of storing data to local so every request for same data is not going to execute PHP and MySQL queries and also speed up the loading of the page AND reduce server load.The Transients API in WordPress is a very useful method for temporarily storing cached data in the database. Transients cache make your WordPress website faster and improve performance of the site.
Transients are temporary means that it has an expiration time and cached data will be deleted after a specified amount of time.
When to use Transients API?
- Complex or custom database queries.
- Static data or information like WordPress navigation menus.
- Transients API is useful to fetch data from external sources like Facebook, Twitter or RSS feeds.
WordPress provides functions to work with Transients.Here I am going to explain about the most used three functions for transient.
get_transient() – To retrieve the cached data
set_transient() – To store data in a cache
delete_transient() – To delete cached data
You can store any data into transient liek array,textobject etc because it is storing information into wp-options
table in serialize format.
set_transient()
This function is used to store data in a transient and have three parameters
SYNTAX:
1 2 3 | set_transient($transientKey, $transientValue, $expiration); |
$transientKey: transientKey is a unique key for your cached data
$transientValue: transientValue is a value or data you want to save. You can use variable, array or object.
$expiration: Number of seconds to cache the data
get_transient()
This function is used to retrieve a stored data in a transient and have one parameter
SYNTAX:
1 2 3 | get_transient($transientKey); |
delete_transient()
This function is used to delete a stored data in a transient and have Transient key parameter.
SYNTAX:
1 2 3 | delete_transient($transientKey); |
Now,Let’s go into depth and understand by code:
1 2 3 4 5 6 7 8 9 | $transient_key = 'transient_key'; $post_data = get_transient( $transient_key ); if ( $post_data == '' ) { $post_data = get_posts( array( 'posts_per_page' => -1 ) ); set_transient( $transient_key, $post_data, 60 * 24 ); } echo $post_data; |
Here, above example is get data using get_transient function and if data is not found, store data into transient using set_transient function with expiration time.
You may also like:
To change File/Image Upload Path in WordPress
To Replace Navigation Text to Icon in WordPress
File Upload with WordPress
If you’re interested in reading more about the WordPress Transients API, I would recommend checking out the Codex page.
I hope you’ve get this is the tutorial of the WordPress Transient API useful and some of what you can do with it.
Let me know if there are any other API examples.