Back to Top

How to use Transient API in WordPress

Introduction to Transient API in WordPress

Nowadays Caching is very important for any website development and WordPress provides own API for caching a data which is known as Transients API.Transients storage API is useful when you are using complex queries in your themes and plugins in WordPress. So Today we’ll talk about how you can use Transients APIs in WordPress itself.

What is Transients API?

Web page data caching is a way of storing data to local so every request for same data is not going to execute PHP and MySQL queries and also speed up the loading of the page AND reduce server load.The Transients API in WordPress is a very useful method for temporarily storing cached data in the database. Transients cache make your WordPress website faster and improve performance of the site.

Transients are temporary means that it has an expiration time and cached data will be deleted after a specified amount of time.

When to use Transients API?

  • Complex or custom database queries.
  • Static data or information like WordPress navigation menus.
  • Transients API is useful to fetch data from external sources like Facebook, Twitter or RSS feeds.

WordPress provides functions to work with Transients.Here I am going to explain about the most used three functions for transient.

get_transient() – To retrieve the cached data
set_transient() – To store data in a cache
delete_transient() – To delete cached data

You can store any data into transient liek array,textobject etc because it is storing information into wp-options table in serialize format.

set_transient()

This function is used to store data in a transient and have three parameters

SYNTAX:

$transientKey: transientKey is a unique key for your cached data
$transientValue: transientValue is a value or data you want to save. You can use variable, array or object.
$expiration: Number of seconds to cache the data

get_transient()

This function is used to retrieve a stored data in a transient and have one parameter

SYNTAX:

delete_transient()

This function is used to delete a stored data in a transient and have Transient key parameter.

SYNTAX:

Now,Let’s go into depth and understand by code:

Here, above example is get data using get_transient function and if data is not found, store data into transient using set_transient function with expiration time.

You may also like:

To change File/Image Upload Path in WordPress
To Replace Navigation Text to Icon in WordPress
File Upload with WordPress

If you’re interested in reading more about the WordPress Transients API, I would recommend checking out the Codex page.

I hope you’ve get this is the tutorial of the WordPress Transient API useful and some of what you can do with it.
Let me know if there are any other API examples.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – June 2013

Posted on 11 years ago

Bhumi

How to set Module Position in Joomla

Posted on 12 years ago

Bhumi