Wednesday , 22 November 2017
Home / Programming / WordPress / Plugins / WordPress Transients API – WordPress Plugin Tutorial
WordPress Transients API
WordPress Transients API

WordPress Transients API – WordPress Plugin Tutorial

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 wpTransientsEx and update the index.php to this.

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

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
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.

About Jasko Koyn

Check Also

WordPress Widgets API

Widgets API – WordPress Plugin Tutorial

The widgets API is what we’ll be learning next. It’s very simple to use. It …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link