I am using wordpress and have created a cookie in functions.php.
My problem is that this cookie is not read by the template/page when it’s created but on it’s 2nd page load.
Is there a workaround to this so I can read the cookie when it’s created?
Here is my current code at functions.php:
<?php
function my_cookie() {
$myvalue = 'hello';
if (!isset($_COOKIE['myCookie'])) {
setcookie("myCookie", $myvalue, time()+3600, "/", ".mydomain.com");
global $myglobal;
$_COOKIE['myCookie'] = $myglobal;
}
}
add_action( 'init', 'my_cookie');
?>
Try setting the cookie from the file index.php. This is the first file that runs when you start your site and so the cookie should then be available by the time you get to the main page.