How can I be sure that a piece of code is executed also for the cached page of the website?

1 Answer 1


You call it asynchronously via the REST API.

In a best case scenario, you want the entire HTML of a page cached (full page cache). Executing PHP inside that is not easily possible and that's exactly the idea behind it

So what you want is some JavaScript that talks to the REST API. The JavaScript code (your "logic") must be written in a way that it does not need to change, so it can be embedded within the cached HTML. Your data that comes from the endpoint can change, because the endpoint is not cached in the same way as a site is.

  • Thanks! I'd like to ask also: if the cache is done through a PHP Plugin, is there any place where I have good chance that Wordpress will execute the PHP code?
    – Revious
    Commented Dec 18, 2019 at 16:29
  • 1
    @Revious That heavily relies on the plugin. I know W3TC offers so called "fragment caching" which is what you're looking for. But I never used it nor do I know if and how other plugins do. As I said: executing part of a cached page is a very tricky thing. In my experience, going the asynchronous JS way is the best solution. As long as it is loaded relatively quickly, it shouldn't be a problem for SEO either.
    – kero
    Commented Dec 18, 2019 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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