Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 .

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 .

share|improve this answer
    
I understand - this is right , when I refresh the page - the cookie is there . I will try to check the JS solution. –  user13279 Dec 13 '11 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.