WP Snippet #008 Using transients to cache data.
Tags: wordpress, webdev, javascript
Published: 2020-03-04 19:44:44

---
title: WP Snippet #008 Using transients to cache data.
published: true
description: A tutorial on how to add custom admin option pages for your Wordpress theme with the Advanced Custom Fields plugin and Php
canonical_url: https://since1979.dev/snippet-008-using-transients-to-cache-data/
cover_image: https://since1979.dev/wp-content/uploads/2020/03/wp-snippet-008-using-transients-to-cache-data.jpg
tags: wordpress, webdev, javascript
---
[Originally posted on my website on March 4th 2020](https://since1979.dev/snippet-008-using-transients-to-cache-data/)

How to cache data using WordPress transients
--------------------------------------------

In the [previous snippet](https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/) we discussed how to use Php to fetch some data from a remote Api. These Api request take time to resolve, and should preferably be cached to speed up page load. In this snippet we will take a look at [WordPress transients](https://developer.wordpress.org/apis/handbook/transients/) which can be used to cache our data.

"The Transients API is very similar to the [Options API](https://developer.wordpress.org/plugins/settings/options-api/) but with the added feature of an expiration time, which simplifies the process of using the `wp_options` database table to temporarily store cached information." ~ [Common APIs Handbook](https://developer.wordpress.org/)

#### Maybe Cache function

{% gist https://gist.github.com/vanaf1979/2a0d5b6b1bd16c9be2ed44d758c3ae09 %}

In the code above we create a new function called *maybeCache* that accepts three parameters.

-   **$cache** (String). Tha name of our cache/transient.
-   **$time** (Int): Time until expiration in seconds.
-   $**callback** (Function): A function that produces the data to cache.

Inside our new function we first check if our data is already cached by calling the *[get_transient](https://developer.wordpress.org/reference/functions/get_transient/)* function and passing it the name of our cache/transient. If there is a valid transient we simply return that data.

We then use the *[is_callable](https://www.php.net/manual/en/function.is-callable.php)* function to check if the passed in callback is a valid function or not. If not we return a error massage using the *wp_die* function.

Next we use the *[call_user_func](https://www.php.net/manual/en/function.call-user-func.php)* function to call the provided callback function and store its return value in a variable *$data*. We immediately pass the *$data* variable along with the *$name* and *$time* variables to the *[set_transient](https://developer.wordpress.org/reference/functions/set_transient/)* function which will .store our cache/transient in the database.

Finally we return the cached $data variable.

#### Using the maybeCache function

In the snippet below we combine our new *maybeCache* function with the *do_remote_get* function from the [previous snippet/article](https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/).

{% gist https://gist.github.com/vanaf1979/b6237218021cf10060f5d3e4e4ae81f6 %}

Here we use the *maybeCache* function and pass it:

-   **remote_posts_data**: As the name of our transient.
-   **7200**: The time in secconds that our cache is valid (7200s = 2 hours).
-   A anonymous callback function.

Inside our anonymous callback function we call the *do_remote_get* function to fetch some posts data from a remote Api and return that data.

On first execution the *maybeCache* function will store the data from the Api inside the database. And on subsequent requests it will return that cached data until our 2 our expiration time has passed, after which the data is invalid and must be called from the Api, and stored again.

Finally, as in the previous snippet, we loop over the posts and echo out their titles.

**Note:** WordPress transient can also be used to cache results of expensive database queries or html fragments that take a lot of processing. But remember that the transients Api uses database queries them self to retrieve their data so be wise and don't over use them.

#### Follow
Found this post helpful? Follow me on twitter [@Vanaf1979](https://twitter.com/Vanaf1979) or here on Dev.to [@Vanaf1979](https://dev.to/vanaf1979) to be notified about new articles, and other WordPress development related resources.

**Thanks for reading**