A major reason for the immense popularity of WordPress is its convenient nature. Whether you are a coder or a non-technical user, you get access to a variety of features that ease your workflow. In this post, we are discussing one such helpful feature called transients. Developers are regularly using the WordPress transients API to store some cached data with time restrictions in their database. Many beginners will not be aware of this technique which can help them in using caching effectively. Here we will discuss this topic in detail and tell how all kinds of users can employ the technique to fulfil their requirements.
What Is Transients API?
Caching is a vital part of your website’s performance. It allows you to store key information in such a manner that it can be immediately served to anyone who requests it. The Transients API is a caching tool that helps users save the data with an expiry time. This means that the information is automatically deleted once that specified date-time is reached. It helps in minimizing the number of queries that your interface has to process. Moreover, defining an expiration schedule helps in automatically cleaning up your database.
Why Should You Use Transients?
Are you an aspiring developer who is still learning about different facets of WordPress? Then you must be wondering why should you use this caching mechanism. There are already tools like caching plugins, available for the purpose. Caching in WordPress is usually about storing a complete web page which is then served to requesting users. However, consider another scenario where you want to cache only an object.
We will try to explain this with an example. Let’s say you want to integrate an Instagram module for your interface. This module will display all your latest posts on the social network. Now, if you make the simple integration, all the posts will be fetched every time the specific page is loaded in a browser. This can affect your website’s performance as the server has to deal with the extra load during every request.
Now, let’s say we use transients to store the retrieved posts and set an expiration schedule of 4 hours. Instead of pulling the posts during every request, they will be served locally. Once the expiry duration is reached, the posts will be deleted and fetched again. Another instance where using WordPress transients can be helpful is when the connection between your Instagram profile and website is not working properly. Then instead of showing no posts, cached data will be displayed on your page.
How To Use Transients?
Generally, transients are stored in the database of your installation. However, tools like Memcached plugins can switch their storage to memory. It will be pertinent, therefore, to never assume your database as the only place to locate transients. They must also be used for storing only that information which will expire after a certain period of time.
There are three basic operations for using transients. Let’s take a look at each one of them.
1. Saving A Transient
When saving a transient, you will have to use set_transient() in the following manner:
set_transient( $transient, $value, $expiration );
Here $transient is the name of the transient which must not exceed 172 characters. $value is the data which needs to be saved and $expiration is the number of seconds after which the data will be refreshed. Look at the example given below:
set_transient( 'box_color', 'Cherry Red', 14400 );
Here, the box color will be cherry red for 14400 seconds or 4 hours.
2. Fetching A Transient
When you want to fetch a saved transient, you will have to use get_transient() in this way:
get_transient( $transient );
Here $transient is the same entity name which we used in set_transient. You can fetch the box color of our example like this:
get_transient( 'box_color' );
Remember if the transient has expired or does not exist, you will get a false value.
3. Removing A Transient
In case, you want to delete your transient before the expiration period then you can use delete_transient() like this:
delete_transient( $transient );
Just like above here too, $transient is the name that we defined in the set_transient. We can remove the transient in our example in this manner:
delete_transient( 'box_color' );
How To Manage Transients?
Are you looking for a convenient way to manage your transients? Then you can do so by using a relevant plugin.
1. Install The Transients Manager Plugin
WordPress website owners are lucky enough to find a plugin for every possible functionality. Here we are using the Transients Manager plugin to manage transients. It is a 5-star rated product with over 10,000 active installations. It is an open-source tool meaning that you do not need to spend any money. Visit your admin dashboard and hover the cursor over Plugins. Then choose Add New and search for the product. Once you locate it, click on install to download it. Activate the tool on your website and move to the next step.
2. Manage Your Transients
A new option called Transients will now be visible under the Tools section in the menu. Click on it to open the plugin’s control panel. Here you will see a list of transients with their names, values, and expiry dates. Use the tabs on top to delete transients in your desired manner.
You will now have got a fair idea of what WordPress transients are and how they can be used. You can use this caching mechanism to improve the performance of your website and ease your own workflow as a WordPress developer.
Disclosure: Brandon Graves is a WordPress developer working with HireWPGeeks