Use Transients API Caching and Speed Up Your WordPress Theme.

Harish Kumar · · 2905 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 to Build Flutter App for any WordPress Site

Leaving a perfect digital footprint is crucial for brand growth. However, you may be here to find the optimal balance between mobile engagement and web traffic. It’s possible (...)
Narola Infotech

The Benefits of Using Hospital Management Software in 2024: A Guide for Healthcare Providers

In the ever-evolving healthcare landscape, integrating technology has become imperative for efficient and effective patient care. One such technological advancement that has revolutionized (...)

Types of Web Applications With Examples And Industry Use Cases

Whether it’s about driving more revenue for your business or strengthening your branding game, an impactful online presence is crucial. To make sure this is done right, there (...)
Narola Infotech

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