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.

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.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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']; 
share|improve this answer
    
Thanks, that did the trick. –  iRector Oct 4 '12 at 7:02
    
The path and domain does not need to be set? –  Damainman Oct 27 '13 at 22:05
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.