I'm trying to set a cookie in Wordpress using a Genesis theme. I'm placing the code at the bottom of the functions.php file and calling the 'isset' to check it's set from a page template.
add_action( 'init', 'my_setcookie' );
function my_setcookie() {
$cookie_name = "touchscreen";
$cookie_value = "touchscreen_1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}
Checking via page template using:
if(!isset($_COOKIE[$touchscreen])) {
echo "My Cookie '" . $touchscreen . "' is not set!";
} else {
echo "My Cookie '" . $touchscreen . "' is set!<br>";
echo "Value is: " . $_COOKIE[$touchscreen];
}
The browser output is My Cookie '' is not set!
Not sure where I'm going wrong whether it's the hook I'm using or Genesis itself? I've seen another couple of articles here but nothing seems to be working.
Update, the cookie is being set I can see it Firefox under, Storage / Cookies... So the issue seems to be accessing it.
No idea why testing it as above does not work in Wordpress but the code below does. The code in my original question was mainly taken from W3 schools, anyway it works now.
if(isset($_COOKIE['touchscreen_1'])){
echo = 'cookie set';
}