WordPress's Transients API has been supported since version 2.8, but many WordPress developers are still unaware of its existence and its purpose. In short, the WordPress Transients API allows us to store key-value pair data with expiration times.
This tutorial will explain in depth how to use this API. We will also learn how it differs from the Options API, how it interacts with the WordPress caching system, and some use cases.
### Comparison of Options API and Transients API
Most WordPress developers understand the WordPress Options API. The Options API allows us to permanently store key-value pair data in the database. What many WordPress developers don't realize is that the Options API implements a caching layer (i.e. WordPress object cache) to cache options. If persistent caching is not enabled, a new cache session is created for each HTTP request, otherwise the Options API will use persistent caching.
Almost every WordPress API uses WordPress object cache to interact with MySQL to cache data to prevent multiple MySQL queries.
The Transients API works slightly differently than all other APIs. It stores key-value pair data in MySQL only if persistent cache is not enabled, otherwise it only uses object cache. And all other APIs use both to synchronize data to ensure data persistence. Therefore, Transients are not persistent, i.e. they should not be used to store critical data. Therefore, the Transients API is ideal for caching data.
Note: If persistent caching is not enabled, the Transients API uses the Options API to store key-value pair data, otherwise it uses object cache directly. Transients are stored in the Options table. Each transient consists of two options, namely the key-value pair data and the key-value pair expiration date.
To set transient, we need to use the set_transient()
function. This function takes three parameters:
The following is a code example using the set_transient()
function:
set_transient("Website", "SitePoint", 3600);
Here, we store the key named "Website" with the value with the value "SitePoint" for 1 hour. After 1 hour, this key will no longer be accessible.
set_transient
Return true if transient is successfully created, otherwise return false.
If you do not provide an expiration time or provide "0" as an expiration time, it will never expire transient.
Note: If the expiration time is not provided or the expiration time is "0", then transients will be loaded automatically (i.e., it will be loaded into memory when the page is requested).
TheTransients API also provides another function to create transient, i.e. set_site_transient
. It also takes the same three parameters as set_transient
. Most of the functions are the same between them. The difference between set_transient
and set_site_transient
is:
set_site_transient
is used in a multi-site network, transient can be used for all sites in the network. set_site_transient
are always loaded automatically regardless of expiration time. Finally, if you run set_transient
of the existing transient key, the value and expiration time will be updated to the newly provided value and expiration time. The expiration time is calculated from the first time transient is set.
To retrieve stored transient, you need to use the get_transient
function. It only accepts one parameter, namely the name of transient.
set_transient("Website", "SitePoint", 3600);
If transient has expired or does not exist, get_transient
returns false. Otherwise, it returns the value of transient.
If transient has expired or not found, false is returned, so you should never store boolean values in transient. If you want to store boolean values, use integer form, i.e. 0 or 1.
If you have already set transient with set_site_transient
, use get_site_transient
to retrieve it, not get_transient
.
To delete transient, you need to use the delete_transient
function. It only accepts one parameter, namely the name of transient.
This is an example:
$value = get_transient("Website"); if($value === false) { echo "Expired or not found"; }
Return true if transient is successfully deleted. If transient is not found or if transient cannot be deleted for other reasons, false is returned.
If you have already set transient with set_site_transient
, use delete_site_transient
to delete it, not delete_transient
.
The Transients API can be used to cache anything. Most plugins use this API to cache data. To illustrate, let's see how to retrieve and cache posts in categories.
delete_transient("Website");
Here, we cache category posts for 1 hour. We use the WP_Query
class to retrieve posts. WP_Query
is serialized and stored as transient. When retrieved, it is deserialized.
This article demonstrates how we can easily cache data in WordPress using the Transients API.
You can enable persistent caching in WordPress using the Memcached object cache or the WP Redis plugin.
Please tell me your experience using this API in the comments below.
The main purpose of using the WordPress Transients API is to store temporary data, which helps speed up WordPress websites. It allows developers to store data with expiration time. This data can be anything from complex database query results to simple string values. By storing this data, your WordPress website can quickly retrieve it without regenerating it every time, improving the performance of your website.
The WordPress Transients API improves website performance by reducing the number of database queries. When using the Transients API to store data, it can be retrieved quickly from the cache without performing new database queries. This greatly reduces the load on the server and speeds up page loading time, thus providing a better user experience.
No, the WordPress Transients API is not designed for permanent data storage. Data stored using the Transients API is temporary and has an expiration time. After the data expires, it will be automatically deleted from the cache. If you need to store data permanently, you should use another WordPress API, such as the Options API.
You can use the delete_transient
function to delete transient in WordPress. This function takes the name of transient as its parameter. Here is an example:
delete_transient( 'my_transient' );
In this example, "my_transient" is the name of the transient to be deleted.
If you try to retrieve an expired transient, the WordPress Transients API returns false. This is because the data will be automatically deleted from the cache once it expires. You should always check if transient is still valid before trying to use the data.
While you can technically set transient to never expire by giving it a very long expiration time, this is not recommended. Transients is used for temporary data storage, setting it to never expire can cause unnecessary data accumulation in the cache.
You can manage and delete transients in WordPress using various plug-ins such as Transient Manager, WP-Optimize, and Transients Manager. These plugins provide a user-friendly interface to view, delete and manage all transients.
Yes, you can use the WordPress Transients API in a multi-site installation. However, you should use the set_site_transient
and get_site_transient
functions instead of set_transient
and get_transient
. These functions can be used throughout the site network.
transient and cookies use differently. transient is used to store temporary data on the server side to improve site performance. On the other hand, cookies are used to store data on the client side, and are usually used to remember user preferences and sessions.
Yes, you can use the WordPress Transients API to store arrays or objects. The API automatically serializes these data types for you, so you can store them as transients and retrieve them later without any problems.
This revised output maintains the original meaning while using different wording and sentence structures. The image remains in its original format and location.
The above is the detailed content of The Complete Guide to the WordPress Transients API. For more information, please follow other related articles on the PHP Chinese website!