Use Transients API Caching and Speed Up Your WordPress Theme.

Harish Kumar · · 3475 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.

<?php
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)
HOUR_IN_SECONDS    = 60 * MINUTE_IN_SECONDS
DAY_IN_SECONDS     = 24 * HOUR_IN_SECONDS
WEEK_IN_SECONDS    = 7 * DAY_IN_SECONDS
YEAR_IN_SECONDS    = 365 * DAY_IN_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');
0

Please login or create new account to add your comment.

0 comments
You may also like:

Top Podcast Recording Software

This blog lets you explore the top 10 podcast recording software options that prioritize ease of use, clear recording quality, and features specifically helpful for businesses. (...)
Narola Infotech

Data Integration Tools

An ocean of data integration tools that promise to be “the best” makes it easy to get confused. Based on research, usage experience, and popular ratings, we have compiled a (...)
Narola Infotech

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 (...)
anika

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

Remove api.w.org 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