A quick and simple way to create a shortcode cache

shortcodes_cache-1038x576

Sometimes, a shortcode must perform a host of time-consuming operations before it can return the intended effect – for example, if we make shortcode that returns Twitter statuses for a specific query. In cases like this there are two solid reasons for creating a cache:

  • getting status information from Twitter requires that the data be downloaded from an external server, which may take a long time.
  • Twitter imposes limits on queries to its API

Fortunately, creating a simple shortcode cache mechanism is very simple in WordPress.

Let’s assume that our shortcode gets 3 different parameters with default values ​​and converts them into a variable with appropriate names in the following way:

$atts = shortcode_atts(array(
'param1' => 'lorem',
'param2' => 'ipsum',
'param3' => 'dolor'
), $atts);

extract($atts);

Our shortcode will then have the following syntax:

[txt][shortcode param1=”x” param2=”y” param3=”z”][/txt]

Something to remember here – our shortcode will return the exact parameter values as is, so a string containing the values of these parameters can confidently serve as an identifier of the shortcode results. In order to store the contents of the cache we’ll use WordPress’ Transients API.

Our cache mechanism will need two key pieces of code – a trace, and a read of the cache content.

Let’s start by reading the contents of the cache, because this operation is performed before the operations used by our shortcode begin:

$cache = get_transient(md5('PREFIX'.serialize($atts)));

if($cache) {
    return $cache;
}

As I mentioned we’re using a string containing the parameters’ values values as an identifier; to be safe, I also suggest that you add your own prefix so that you are secure should all the parameters be empty. If there is the cache for a given configuration, then the function simply returns the contents of the cache immediately, without performing additional operations.

Next, we put the output code generated by the shortcode in a variable such as $cache_output – if you create your shortcode correctly, then you should already have such a variable for the result content (you don’t use echo in shortcodes, do you?). We can place the content of this variable in the database at the end of our shortcode’s actions:

set_transient(md5('PREFIX'.serialize($atts)) , $cache_output, 10 * 60);

Thanks to the addition of just six lines of code, we have a simple cache mechanism ready to go.

Personally, I recommend you to add a another parameter, cache_time, to the list of shortcode parameters – so that you will be able to determine the cache storage time. The last line associated with the cache will change to:

set_transient(md5('PREFIX'.serialize($atts)) , $cache_output, $cache_time * 60);

This solution can be advantageous in shortcodes where there is little variation of the parameter values, as even when storing the results of several thousand entries only several-dozen records in the database will be needed. In contrast, I suggest caution in the event that each of a number of entries have unique shortcode configurations as there is a risk that the number of records in the database will be similar to the number of entries.

Share
This article was first published July 25th, 2014