How to disable cache for the content returned by a shortcode

admin2025-01-08  5

we have wrote a shortcode that returns a different content based on a timestamp.

The typical business case is: "Before the 1st of January, display "registrations will be open on the 1st of January" and after that date "register here""

Therefore, it is quite important that this piece of content does not get cached.

Is there a way, from the shortcode code to indicate that this piece of content should not get cached (knowing that the cache mechanism is not yet chosen - nginx, memcache, total cache, ...)?

we have wrote a shortcode that returns a different content based on a timestamp.

The typical business case is: "Before the 1st of January, display "registrations will be open on the 1st of January" and after that date "register here""

Therefore, it is quite important that this piece of content does not get cached.

Is there a way, from the shortcode code to indicate that this piece of content should not get cached (knowing that the cache mechanism is not yet chosen - nginx, memcache, total cache, ...)?

Share Improve this question asked Oct 10, 2017 at 8:20 E. JaepE. Jaep 1091 silver badge4 bronze badges 4
  • I'm afraid that It totally depends on the chosen cache mechanism, method or plugin. – cybmeta Commented Oct 10, 2017 at 8:34
  • If you load the content in the shortcode via AJAX/REST with a script, then it would not be affected by caching. – Jacob Peattie Commented Oct 10, 2017 at 10:08
  • @JacobPeattie Thanks for the reply. I assume that the script would have both versions of the text and display them according the passed parameter. correct? How would you make sure that the text is not "retrievable" by simply displaying the page source? – E. Jaep Commented Oct 10, 2017 at 10:45
  • The shortcode will need some sort of unique identifier (like a post ID if its referring to a post), then there would be a script that would send an AJAX request with that ID. The AJAX handler would then use that ID to return the correct text. The script would take the response and output it on the page. – Jacob Peattie Commented Oct 10, 2017 at 10:48
Add a comment  | 

1 Answer 1

Reset to default 0

As the caching usually doesn't work on parts of the page, but on the whole page, you can't have it just not cache that specific part (well, you could use ESI/Edge Side Includes, but that's not an option for all implementations).

What you could to is write a hook that checks whether these shortcodes are used and adjust headers indicating that it might not be cached or only cached up to X seconds (with the indicated max lifetime falling on the date and time you want to show the changed content).

Or you could parse the shortcodes when posts are updated and add WP cronjobs that will silently update the post. You'll most likely have some sort of purging mechanism in your cache implementation that removes the cached result if the post is updated.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270027a1397.html

最新回复(0)