Q:

Issues with set cookie

At first I had my setcookie() as:
$school = "Some Value";
setcookie("default_school", $school, time()+3600);
var_dump($_COOKIE);

That didn't work, so I did some searching, and found that I probably just needed to set a path, so I changed it to:
$school = "Some Value";
setcookie("default_school", $school, time()+3600, "/", ".mydomain.com");
var_dump($_COOKIE);

That still didn't work so I did some more investigating, and determined that my issue must just me setting the cookie too late. So I then started checking my if my headers were set in certain parts of my code, which needs to return bool(false).
var_dump(headers_sent());

First the top of my index.php, then at the top of my header.php, but still the same result; they both return true?
I feel like this should be so simple, and I'm getting nowhere. A push in the right direction would be highly appreciated.
Thanks,
Isaac
EDIT: Just tried putting it before the init hook, and it returned false. I then put the setcookie function there, and still no luck.

A:

If you’re doing this via a theme, add this line to your functions.php file:
function set_user_cookie() {
    $school = "Some Value";
    setcookie('default_school', $school, time()+3600);
}
add_action( 'init', 'set_user_cookie');

To display information add this line
echo $_COOKIE['default_school']; 

