How to cache a shortcode functions output?

You might have a look at the WordPress Transient API.

You should be able to store your shortcode output $total_network with

set_transient( 'my_shortcode_cache', $total_network, WEEK_IN_SECONDS );

where WEEK_IN_SECONDS is a built in constant equal to 604800.

You can then fetch it with:

get_transient( 'my_shortcode_cache' );

If you need it to work network wide there exists also set_site_transient() and get_site_transient().

Leave a Comment