Cache Your Shortcode in WordPress

In your short code, check if a cached version of the shortcode output is already cached prior to building the output:

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

if ( $cache ) {
  return $cache;
}

After the shortcode has generated the output, save the output and set a time limit before you return the output.

set_transient(md5('SCF_'.serialize($atts)) , $output, 30 * 60); // cache output

reference: https://www.gavick.com/blog/create-shortcode-cache-simply

Scroll to Top