The Cache Dynamic Shortcode offers a method for temporary data storage, enabling the caching of content within WordPress. Its main benefit is to boost performance by reducing database load and cutting down on page loading times.
This shortcode allows for the caching of other Dynamic Shortcodes, and it simplifies the process of storing, fetching, and erasing cached entries with customizable expiration periods.
Syntax
The syntax for implementing the Cache Dynamic Shortcode is as follows:
For creating/updating cached data:
{cache:<key> <value>@<key_arguments>}
For retrieving cached data:
{get-cache:<key>}
For deleting cached data
{delete-cache:<key>}
Key Arguments
expiration
(aliasexp
): Determines how long the data should be kept cached, specified in natural language like "30 minutes", "1 day" or "2 weeks".
Examples
Caching Data:
{cache:current_weather "Sunny"@expiration="30 minutes"}
This example caches the string "Sunny" with the keycurrent_weather
for 30 minutes.Retrieving Cached Data:
{get-cache:current_weather}
Retrieves whatever data is stored under thecurrent_weather
cache key.Caching with Default Expiration:
{cache:website_stats "1500 visits"}
Stores "1500 visits" underwebsite_stats
with a standard expiration time.Dynamic Caching with ACF:
{cache:user_bio {acf:user_bio_field}}
Caches the output of an ACF fielduser_bio_field
under the cache keyuser_bio
.