Use Transients API Caching and Speed Up Your WordPress Theme.

Harish Kumar · · 2563 Views
Use Transients API Caching and Speed Up Your WordPress Theme.

The Transients API in WordPress is an effective method for saving cached data in the database. It allows us to take resource-intensive queries and store them in short-term caches to enhance performance. Transients are short-term. For example, after a specified period of your time, the cached information will be removed and re-cached, or modified.

This quick demonstration will walk you through how to use transients in your WordPress development.

Dealing with transients is straightforward. There are just three functions that you need to know about:

  1. set_transient() – Used to store data in a cache

  2. get_transient() – Used to retrieve the cached data

  3. delete_transient() – Used to delete cached data

Usage Transients

1. Saving Transients

set_transient() function used to save or update the value of a transient in the cache.

set_transient( $transient, $value, $expiration );

$transient is a unique key name for your cached data.

$value Data to save, either a regular variable or an array/object. The API will handle the serialization of complex data.

$expiration The maximum of seconds to keep the data/value before refreshing.

Example: Saving the $query_results object for 12 hours:

set_transient( 'query_results', $query_results, 60*60*12 );

Using Time Constants

In WordPress 3.5, few time constants were introduced for easy time express.

MINUTE_IN_SECONDS  = 60 (seconds)

Example of Time Constants:

set_transient( 'query_results', $query_results, 12 *  HOUR_IN_SECONDS);

2. Fetching Transients

get_transient() function is used to get the saved transient.

<?php get_transient( $transient ); ?>

Here $transient is a Unique transient key name. If the transient does not exist or does not have any value, then it will return a false value.

3. Removing Saved Transients

delete_transient() function used to delete manually the transient key before it expires.

delete_transient( $transient );

$transient the unique key name used when saving with set_transient().

The function below will demonstrate basic usage of transients:

function qirolab_transient_demo() {
	// get the cached data
	$data = get_transient('sample_trans');
	// check to see if data was successfully retrieved from the cache
	if( false === $data ) {
		// do this if no transient set
		$data = 'This is the data stored in the transient';
		// store the data and set it to expire in 10 seconds
		set_transient('sample_trans', $data, 10);
	// this will output "This is the data stored in the transient" regardless of whether a the data was retrieved from the cacue or not.
	echo $data;
add_action('admin_notices', 'qirolab_transient_demo');

Please login or create new account to add your comment.

You may also like:

How Can I Find Best WordPress Development Company In India 2023?

According to me there are various companies who are doing WordPress development but some of them are providing reliable word press development solutions.
Kenny William

Remove REST API/JSON API from WordPress header.

WordPress uses the REST API since edition 4.4 of the CMS. It allows developers to interact with the WordPress back-end more quickly since this API is a standard way to connect. (...)
Harish Kumar

How to Add Custom User Profile (User meta) Fields In WordPress

When you are focusing on tasks that need user management, and you need to add more fields for the user. In that case, here user meta functionality is used. This is similar to creating (...)
Harish Kumar

WordPress: How to Fix Missing required field entry-title, Update, hCard Error in Google Structured Data tool.

Recently when I tested one of my WordPress weblogs via Google Structured Data testing tools, I got the following errors:
Harish Kumar

How to fetch Any post with WP_Query in WordPress?

WP_Query is your buddy. It allows you to get content from the database according to your requirements. In this article, I will explain top to bottom about how WP_Query works. let’s (...)
Harish Kumar

How to Enable Featured Image in WordPress?

Featured Images or Post Thumbnails is a theme feature. Most themes such as Genesis and other themes support featured images by default. A great way to determine whether your theme (...)
Harish Kumar