I am trying to set a cookie on click via

 setcookie('mycookie', 1, time()+60*60*24;

but I can not seem to get it working . If i click the button - when I check my cookies, it is not there .

1 Answer 1


Cookies are set on the session BEFORE the page starts to load. Therefor - This function will set the cookie for the NEXT time the page is requested.

you can not (as far as I know ) set a cookie onclick without any javascript help, becasue when at the time the page is rendered (and your click button) - all the cookies are already set.

If you are familiar with jQuery - there are some very good plugins that can help you set a cookie via JS and "refresh" the cookies session. Some of them , will actually refresh the cookie in a way that will trigger also the "normal" wp cookie to be updated .

  • I understand - this is right , when I refresh the page - the cookie is there . I will try to check the JS solution.
    – user13279
    Commented Dec 13, 2011 at 5:52

Your Answer

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