This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Today, we’ll be discussing how to use the WordPress transients API. Most bloggers don’t use WordPress out of the box. They’re usually using plugins ranging from completely custom to ones they found in the WordPress plugin directory. If you’re a blogger, you’re usually adding fresh new content daily. With that being said, you as the developer, must find a way to make WordPress blogs run fast and optimal. Performance is absolutely important.

So, how can you make WordPress run faster? This is where the transients API can help you. Some data on WordPress doesn’t have to be updated constantly. So, the best thing to do is cache it and check every so x seconds or minutes to update the data. This may seem intimidating at first, but the transients API actually makes this process a breeze.

What is the WordPress Transients API?

The WordPress transients API allows you to cache data. It’s very similar to the options API except that all data has an expiration timer. The transients API increases WordPress’s performance greatly. The best part, it’s easy to use. You only have to remember 3 functions in order to start using this API.

Getting Started

Create a new plugin called wpTransientsExand update the index.php to this.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?php

/*

* Plugin Name: WP Transients API Example

* Plugin URI: http://jaskokoyn.com

* Description: A Basic WordPress Plugin to show the WordPress Transients API works.

* Author: Jasko Koyn

* Version: 1.0

* Author URI: http://jaskokoyn.com

*/

// Set up our WordPress Plugin

functionjk_check_WP_ver()

{

if(version_compare(get_bloginfo('version'),'3.1','<'))

{

wp_die("You must update WordPress to use this plugin!");

}

}

register_activation_hook(__FILE__,'jk_check_WP_ver');

// Include or Require any files

include('inc/func.inc.php');

// Action & Filter Hooks

add_action('wp_footer','jk_transient_footer');

?>

Nothing new here. Same code as usual. Let’s create and open up our func.inc.php file and define the jk_transient_footerfunction.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?php

functionjk_transient_footer()

{

$message_array=array('This is message 1','This is message 2','This is message 3');

shuffle($message_array);

$message=get_transient('jk_random_message');

if(!$message)

{

set_transient('jk_random_message',$message_array[0],60);

$message=get_transient('jk_random_message');

}

echo$message;

}

?>

Let me explain what’s going on here. We create an array with 3 messages. We then use the shuffle() function to mix up the values inside the array. More information about the shuffle() function can be found here.

We then use the get_transient() function to get a transient. This function will return false if 1 of 2 things happen. If the transient that’s being called for is expired, it’ll return false. If the transient doesn’t exist in the first place, it’ll return false. On the first load, this will return false. If we do get false, we’ll create the transient.

The set_transient()function has 3 parameters. The first parameter is the key. This is the name you’ll use when you want to grab this transient. The value put in here will correspond with the value you put in the get_transient()function. The second parameter is the value for this key. In our case, the first item in the $message_array that was shuffled earlier. The last parameter contains the amount of seconds this transient will expire from the moment of it’s creation. We set this to 60 seconds. So, in 60 seconds, this transient will expire, which will cause WordPress to set the transient again.

You’ll notice we’re grabbing the transient again after we create it. Since the first time returned false and we just created it, we have to grab it again if we want to use it again. We then echo out the message.

There’s one more function associated with the transient API. This is the delete_transient() function. You pass in the name of the transient you would like to delete. You won’t find yourself using this function unless you want to delete the transient before it expires on certain conditions. It’s totally up to you.

That’s it! Activate your plugin and refresh the page every 60 seconds to see if you get a new message outputting to the footer.

WordPress Transients API

Conclusion

The WordPress transients API is very useful if you’re constantly loading data that’s large and isn’t always fresh. You can really speed up the performance of any WordPress blog by using this API. If you would like more information about the WordPress transients API, click here.