A cache plugin in WP will cache PHP output, but does it include HTTP header? For example, add a piece of code as following:

add_action('wp', 'my_cookie' );
function my_cookie() {
    setcookie('my_cookie', 'value');

Will my_cookie be saved to client browser when served by cached page?


It will most likely depend on the plugin, but you should also remember that your content might be cached also in caching proxies as well. https://stackoverflow.com/questions/9334393/proxy-cacheing-what-about-cookies

In other words, if your output is different to two different users, than you can not use caching. The way to go around this is to make the decision to set the cookie in JS if possible.

  • It depends, but I think it weird/difficult for a cache plugin to ignore part of PHP code, right? So I guess setcookie will work, of course, for all users. JS is convenient, but I found it slow even I set it in header, a script in footer has to use window.load to get the cookie. – Edward Apr 25 '17 at 3:29
  • Whether it is weird or not is not the issue. The issue is that there is no easy way in php to intercept cookies being set, so what you are asking is whether a plugin author will go the extra mile to be able to do it (if possible at all). The real issue is that since plugins are unlikely to do it, but caching proxies might do it, you will get different behavior depending on whether someone accessed the site "directly" or via a proxy. – Mark Kaplun Apr 25 '17 at 3:39
  • It would be a problem if the cookie is used to serve different content to users, but I use it for passing parameter in an ajax request, so it doesn't matter if it be cached, but does matter if it not be cached, in which case, the server side code will not get the parameter. – Edward Apr 25 '17 at 3:55

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.