I use only "pages" from my WP installation (not posts) and I want to set a cookie based on the existence of a GET parameter in the URL.

I have put the following code in the top of my page.php:

if ( get_query_var('value') ) {

  setcookie( 'value',$value, time() + 2592000, COOKIEPATH, 'domain.com' );

So if I visit url like: www.domain.com/page/?value=1234 I want to have a cookie: value: 1234 set for this domain.

The problem is that this doesn't work all the time (seems to be working about 70% of the time), and sometimes the cookie is not set.

I have Autoptimize and WP Super Cache plugins enabled, and CF as CDN.

What I tried: I'm far from expert in WP, but I understand that there is also the option to add a function to functions.php with the same purpose, but I didn't managed to make it work at all like this.

Does it matter where in the body of page.php I put this code (should it be more down so that it always get executed?), or is there any other way to make sure the code is executed all the time.

I know that there are ways of setting cookie from JS as well, but I would preffer to do it from php.

  • 2
    If it works 70% of the time, then your code works, but perhaps the page is cached, hence the cookie doesn't get set - i.e. your PHP is not called and your caching plugin simply loads the cached output of the page.
    – Sally CJ
    Mar 3, 2021 at 16:32
  • yes, I'm suspecting one of the plugins (unfortunately not installed by me, but this is why i listed them in my question), but is there a way to force this being executed? the only alternative is to do it from JS?:(
    – NewGuy
    Mar 3, 2021 at 16:46
  • Maybe there is, but you might instead want to just disable caching for the page in question, or for URLs with that value query. However, please consult the documentation or support articles/forums/site for the caching plugin that's used on your site.
    – Sally CJ
    Mar 3, 2021 at 17:02
  • We used to do that, but ran into the same problems with caching. Unfortunately you're right, you have to do it from JS. You can't reliably do this from PHP with any cached WordPress. You would be able to configure your cache to parameterise the page on the value parameter but JS is the better way.
    – Rup
    Mar 3, 2021 at 19:17

1 Answer 1


Answering my own post here, it seems that WP Super Cache has a dedicated setting (in Advanced section) to not cache urls that have a GET parameter. Once that is checked, problem seems not to replicate for me anymore.

So indeed the root problem is that cached files do not run the php code anymore, hence setting cookie from php fails and probably JS is a better option.

There might be other ways around it to force cookie setting specifically, look for example at wpsc_add_cookie functionality.

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.