Relaxed-Kitten
Webprogramo > Blog > Wordpress > Demystifying WordPress Transients. Just Relax =).

Demystifying WordPress Transients. Just Relax =).

Yup, just relax like this Kitten =)

Alejandro Lopez

23 julio, 2017

Webprogramo > Blog > Wordpress > Demystifying WordPress Transients. Just Relax =).
SHARE! and the author will get a Yummy Cookie :
Share this...
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter

Hey! Relax =), take a big breath and repeat after me «WordPress Transients are easy, it is just a weird word.» Do it a couple of times, and keep reading… Yes! We are going to Demystify WordPress Transients, If you haven’t heard about it, you are lucky, now, you are going to learn about this mysterious WordPress word as comfortable as possible.

Let’s start saying this «Try to forget the transient name, and learn ‘cached info’ instead,» why? Because transients are just a way to take information and save it in your database to be cached for a defined time or forever! And, this is the very moment when you say «Ooohhh =O, that’s all?», Yes! That is everything.

So, let’s stop talking and show some code with WordPress Transients!

To create a cached info in your database, you do this:

set_transient( "id_info_to_save", $info, $time);

The first parameter is the name you are going to give to your data. The second parameter is your data; it can be an array, an object, a string value, etc., just some information you want to save. And about the last parameter and it is essential; The time IN SECONDS you want your information to be available, it is your expiration time. You can set it to 0, if you want that cached info to be saved forever or until you removed it manually.

Thanks to the Awesome WordPress, there is a list of constants from version 3.5 who represent some times in seconds and you can use them in your expiration parameter, and they are:

MINUTE_IN_SECONDS = 60 (seconds)
HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS
DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS
WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS
MONTH_IN_SECONDS = 30 * DAY_IN_SECONDS
YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS

So, you can create a transient to be available for a whole week, like this:

$my_info = array(
    'name' => 'Alejandro Lopez'
    'age' => '1.000.000 years',
    'job' => 'Web Developer'
);

set_transient( 'my_info', $my_info, WEEK_IN_SECONDS );

To retrieve the information you have saved, you just do:

get_transient( 'my_info' ); // Yes, that's everything.

And to remove it (It is removed by default when it reaches the expiration time), you just execute.

delete_transient( 'my_info' );

NOTE: «my_info» is just the ID of your information saved in your database.

LOVELY! Am I right? Now, you can go to sleep comfortably and with no worries, because one of your ghosts is gone!

Postdata

There are three more functions set_site_transient(), get_site_transient(), and delete_site_transient(); they work exactly like the normal transient I explained in this article, they just have two differences:
First, they are transients for your whole network if you have the multisite of WordPress enabled.
Second, they are loaded by default always.

To take a look at WordPress Transients, in the WordPress Codex, go to Transients API