This is my first time playing around with cookies, so i apologize if the question is "stupid", but i did not manage to find an answer anywhere else on this site.

Iv'e set a cookie using this code in my child theme functions.php file:

$_fa = 'friend';

if(!isset($_COOKIE['myCookie'])) {

// set a cookie for 1 year
setcookie('myCookie', $_fa, time()+31556926, "/");

$_COOKIE['myCookie'] = $_fa;


The cookie is set site wide, with the correct value, and I can access the cookie and its information in chrome by looking at the cookies manually.

The problem is:

If i am browsing the website while I am logged in, the functions that depend on the cookie, runs smoothly, as in, they manage to fetch the cookie name and value to perform different tasks.

But, when i log out and browse the website as a random visitor, none of the functions fire, becouse they do not detect the cookie name or value at all.

I also ran a test to confirm, with this code:

if(!isset($_COOKIE['myCookie'])) {
echo "Ooops! Cookie not found!";
} else {
echo 'Hello ' . htmlspecialchars($_COOKIE["myCookie"]) . '!';

This returns:

Ooops! Cookie not found! When I am browsing as a random visitor "not logged in".


Hello friend! When I am browsing while logged in as a WP user.

This happens on every page on the website and in Chrome, Edge and Firefox (these are the browsers I've tested)

Just to clarify, the cookie is ALWAYS in the browser. It's not deleted when i log out or anything like that. It's just that none of the functions or statements detect the cookie once i browse the site, without being logged in.

What in my code is creating this behavior, and what can I do to fix this issue?

1 Answer 1



This was something I had been trying to debug for the last couple of days. And the answer was found by mistake.

What caused the error was caching. I had to exempt the cookie from cache in order for it to work as it should.

So, if you are experiencing this behavior and start a debug quest that you know will bring you pain and misery, check your server settings or give your hosting provider a call first.

  • Thanks! I had to contact my host (WP Engine) and have them add exclusions for certain cookies.
    – Gavin
    Commented Oct 31, 2021 at 14:44

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.