Turning WordPress Shortcodes into ESI Blocks

You can turn WordPress shortcodes into ESI blocks with LiteSpeed Cache. This allows you to cache the contents of the shortcode in a different way than you've cached the rest of the page. (You can learn more about ESI on our blog, if this concept is new to you.)

If you have a mycalendar shortcode, for example, and it inserts a calendar into your page, you might use it like this:

[mycalendar month="November" year="2018"]

To turn it into an ESI block, you would instead use it like this:

[esi mycalendar month="November" year="2018"]

By default, shortcode contents are stored in public cache, and the TTL defaults to whatever value you have stored in Settings > General > Default Public Cache TTL, but you can change that with a few parameters. To store the shortcode contents in private cache for five minutes (or 300 seconds), you can say this:

[esi mycalendar month="November" year="2018" cache="private" ttl="300"]

While LiteSpeed Cache can easily cache your shortcode contents, it is not possible for LSCache to purge the shortcode contents on demand. Shortcode ESI blocks can naturally expire when the TTL is reached, but a purge cannot be triggered by particular events. This makes sense, because LiteSpeed can't know which occurences should trigger a purge. Different shortcodes all have different events that render them out-of-date, and there's no way for LiteSpeed to know what they are.

Using the example of the calendar plugin above, let's say you use the following shortcode:

[esi mycalendar month="November" year="2018"]

This will cache the mycalendar block for the same length of time as your site's default TTL. If someone edits an event before the TTL is reached, then the ESI block will, unfortunately, be out-of-date.

There are two ways to handle this issue:

  • Have the shortcode's plugin author use our API to trigger a purge when block content changes.
  • Use a short TTL, and live with the possiblity that contents may be out-of-date for a short time.

Get the Plugin Author Involved

If it's important that the shortcode contents be purged by specific events, you can share this API call with the author of the shortcode's plugin (just be sure to replace mycalendar with the actual name of the shortcode you want to purge:

method_exists( 'LiteSpeed_Cache_API', 'purge' ) && LiteSpeed_Cache_API::purge( 'esi.mycalendar' ) ;

This is the most precise way to keep the content in the shortcode up-to-date and accurately cached according to the shortcode's own requirements.

Do it Yourself

If it is not critical for the contents of the shortcode to have up-to-the-minute accuracy, then you can use the ttl parameter to cache the content for a short time. If you can live with content that is an hour old, set ttl=“3600”. If you are thinking more along the lines of five minutes, set it to ttl=“300”.

While it is possible to set the content to not be cached at all ( ttl=“0”), it is not recommended. Any time there is uncached content on a page, PHP must be invoked in order to generate that content. PHP uses valuable resources, and significantly slows down a page. It's far better to cache your content for a small amount of time than to set it not to be cached at all.

  • Admin
  • Last modified: 2018/11/30 20:52
  • by Lisa Clarke