Skip to main content

WordPress Transients: What are they and how do I eat them

Published ago
Updated ago
8 min read
Warning

This article was written in 2011 👴🏼. Most of the tips here may not be valid anymore.

I'm keeping this article here for historical reasons and to remind myself how much I've improved over the years.

Only cosmetic changes were made to this article.

WordPress, despite being my favorite daily work tool and the one I know the most, has still several options and functions I've never used. Not too long ago, one of them was the Transients API. Really useful functions to store temporary data in the database.

What are Transients?

In WordPress language, a transient is any piece of temporary data, that means we expect it to be deleted. This is useful if we have information we're going to use for just a small amount of time and we don't want to fill the database.

Lets say we have a contact form (or comments) where the user will type the name, email, website and comment, once they're typed the form is sent, 5 minutes later the user finds an error in our site and wants to get in touch again, does he have to fill ALL the fields over again? Usually, yes, but using Transients we can spare him the trouble.

The Code

The Transients API and the Options API are a lot alike, and also really easy to use, in fact, it's conformed by only three functions:

  • set_transient( $transient, $value, $expiration): The function that creates our transient, where:
  • $transient is the transient's unique name, this is important since it's the key to find the transient later.
  • $value is the transient content, it can be a string or an array.
  • $expiration is the important part here, it's the time (in seconds) our data will stay in our database, once time's up, the transient will be deleted.
  • get_transient( $transient ): The function we use to call our transient, where $transient is the unique name we set before.
  • delete_transient( $transient ): Even though transients are deleted automatically when they're out of time, it might be the case we want to delete them manually, where $transient is still the unique name I talk so much about.

How are they used?

Following the previous situation (the contact form), I'll show a little example of transients use, nothing to be worried about, we start with the form:

1<form id="transient_test" action="process.php" method="post">
2 <fieldset>
3 <label for="name">Name:</label>
4 <input id="name" type="text" name="name" />
5 <label for="email">Email:</label>
6 <input id="email" type="email" name="email" />
7 <label for="url">URL:</label>
8 <input id="url" type="url" name="url" />
9 <label for="comment">Comment:</label>
10 <textarea id="comment" name="comment"></textarea>
11 <input id="submit" type="submit" name="submit" value="submitted" />
12 </fieldset>
13</form>
14

This will give us a fairly simple form, we take Name, Email, URL and Comment. Also note I'm using HTML5 attributes, don't worry, I'm no hipster.

Once we have the data we must of course process it, to do that we'll need a php file I've called process.php, this one will work more or less like this.

1<?php
2 if ( isset( $_POST['submit'] ) ) { // Make sure the form was submitted
3 $name = $_POST['name'];
4 $email = $_POST['email'];
5 $url = $_POST['url'];
6 $comment = $_POST['comment'];
7 $time = 60*60*24; // Expires in 1 day *
8 $this_transient = get_transient($name . '_' . $email); // Search some transient
9 if (!$this_transient ) { // If doesn't exist
10 set_transient('name_' . $email, $name, $time);
11 set_transient('mail_' . $email, $email, $time);
12 set_transient('url_' . $email, $url, $time);
13 }
14 $message = "Name: " . $name . "nnE-mail: " . $email . "nnURL: " . $url;
15 $headers = 'From: ' . $name;
16 wp_mail("me@me.com", "Transients Test", $comment, $headers); // Send a mail
17 }
18?>
19

Ok, now this code will take the data we send through the form and search the database for the key value (our mail), if it doesn’t exist it will create new transients with names like name_me@me.com using the data, these will expire in 24 hours. After that it will send an email with the comment.

Note

$time must be a time value in seconds, since I have no idea how many seconds are in a day** it's easier to let PHP itself to do the math, this is 60 seconds × 60 minutes × 24 hours, even easier: **60″ × 60′ = 1 hour × 24 = 24 hours**.

All right, we have the code to store the transients, now what? Right, we needed them so the user doesn't have to type them over and over again in the form, in order to do that we have to modify our form and add a cookie:

1$cookie = $_COOKIE['transients_test'];
2<form id="transient_test" action="process.php" method="post">
3 <fieldset>
4 <label for="name">Name:</label>
5 <input id="name" type="text" name="name" value="<?php echo get_transient('name_'.$cookie); ?>" />
6 <label for="email">Email:</label>
7 <input id="email" type="email" name="email" value="<?php echo get_transient('name_'.$cookie); ?>" />
8 <label for="url">URL:</label>
9 <input id="url" type="url" name="url" value="<?php echo get_transient('name_'.$cookie); ?>" />
10 <label for="comment">Comment:</label>
11 <textarea id="comment" name="comment"></textarea>
12 <input id="submit" type="submit" name="submit" value="s ubmitted" />
13 </fieldset>
14</form>
15

Now the form will check the existence of the cookie called transients_test, which doesn't exist right now, to create it we have to add the following to the process.php file:

1setcookie('transients_test', $_POST['email'], time()+86400, '/', $_SERVER['HTTP_HOST'], 0, 0 );
2

That will create the cookie called transients_test, with the user's email, will expire in 86400 seconds (total seconds in one day) and will be valid for our entire site.

That's it, we have our contact form which remembers our visitors for one whole day, after that their data will be erased from our database until the next time they fill and send the form.

Notes

Usually the transients are stored in our database table wp_options, but they might not, some plugins may change they location, that's why it's recommended to use transients only with data expected to be erased. If you want to keep it, use other method.

Further reading