0

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, ...)?

  • I'm afraid that It totally depends on the chosen cache mechanism, method or plugin. – cybmeta Oct 10 '17 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 Oct 10 '17 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 Oct 10 '17 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 Oct 10 '17 at 10:48
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.

  • The option to add a filter hook modifying the page's header seem to be the most generic to me. However, I cannot find any suitable filter hook in WordPress' list (codex.wordpress.org/Plugin_API/Filter_Reference). Would you have any recommendation? – E. Jaep Oct 10 '17 at 11:55
  • Since you might have to deal with other plugins' headers and change/remove them, I'd opt for wp_headers and add them that way. You could also use send_headers, which is run after other headers have been sent. Using header(), the last call wins and overwrites earlier headers with the same name. – janh Oct 10 '17 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.