Content of cookie is being destroyed when navigating to another page within the same site

Question

I am trying to set up a type of referral system for my site.

Our partner will direct users to our site using www.oursite.com/?ref=partner

I am then setting a cookie in my functions.php using:

add_action( 'init', 'set_new_cookie');
function set_new_cookie() {
    $value=$_GET['ref'];
    setcookie("TestCookie", $value, time()+3600, '/');  /* expire in 1 hour */
}

It works fine initially and I can see the correct value populated in Chrome’s cookies.

However, as soon as I navigate to another page within my site the Content of the cookie is lost and reads as deleted.

I thought the cookie was supposed to persist for an hour?

What am I missing here?

Is my set_new_cookie function running each time I navigate to a new page? If so, how can I get it to run only once on the initial site visit?

Is there something WordPress specific that would be causing this behaviour?

0
chewflow 2 months 0 Answers 17 views 0

Leave an answer