If you use WordPress, you may have heard this word at least once: “transient”.
Good, you already achieved step one. But do you really know what transients are, and to use them?
I this quick post I’m going to explain what transients are and how to use them in your own developments. First of all, and this should be your first reflex, let’s go to the Codex.
First thing to note: transients is an API. And here is how the Codex describes it:
This page contains the technical documentation of the WordPress Transients API, which offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.
I guess it’s a bit clearer now isn’t it? In other words, the transient API is a perfect way to store your own variables for a limited period of time before they self-destruct. I already talked about transients on my website, explaining how to cache the result of a query using transients, but today we are going to make a more basic use of the API.
I recently did something pretty interesting: as a Codecanyon author I wanted to show on my “About me” page the number of happy clients I have. As Codecanyon is a marketplace, the number of happy clients increase every day, and I can’t update the value manually everyday. So, I decided to use the Envato API (Envato is the company behind many marketplaces such Codecanyon and Themeforest) to retrieve my sales value, store this value in my database and refresh this value every 12 hours. This is exactly what transients API does.
I’m not going to explain the process to retrieve values from Envato API, so let’s say that the sales number is stored in a $envato_happy_clients variable. Here is what the code would be:
<?php // Cache timeout in seconds $CACHE_EXPIRATION = 60*60*12; // 12 hours // Transient ID & cached data $transient_id = 'remi_happy_clients'; $happy_clients = get_transient( $transient_id ); // Is data already in local or new call to API if ( !$happy_clients ) { /* Envato API call to retrieve sales number. Let's assume the result is $envato_happy_clients */ $happy_clients = $envato_happy_clients; // Set the transient - cache item data set_transient( $transient_id, $happy_clients, $CACHE_EXPIRATION ); } echo $happy_clients; ?>
And the result is something like you can see on my “About me” page. So, basically, we define in $CACHE_EXPIRATION the period we want to keep the cached data, we then place the content of the transient in $happy_clients variable and then we check if the value is set. If not we connecte to the Envato API to retrieve fresh information, otherwise we simply echo the happy customers value.
Well, this is it! It’s simple but powerful, and I really encourage you to have a deep look to this API that can do much more!
SeedProd is one of the elders of the WordPress ecosystem. It's also a bit of…
Really Simple Syndication (RSS) feeds are a vital component of the WordPress ecosystem. They act…
It’s that time of year again when people start revving up their shopping lists, hunting…
If you clicked on this post, I’m guessing you want to find the best solution…
Trying to figure out how to start an online course so that you can share…
Considering using Teachable to create an online course? In our hands-on Teachable review, we'll help…
View Comments
Really cool
I tried to do something like you but it has increased my Queries (20 to 23 but if I disable my custom query it give me 15 queries)
Here is my code :
$exemple = get_transient('exemples');
if (false === $exemple) {
$exemple = new WP_Query('cat='.$catss.'');
set_transient('exemples', $exemple);
}
while ($exemple->have_posts()): $exemple->the_post();
Thanks for your help