I am trying to create some functionality in a WordPress site that will create a cookie when a user visits a location page that stores the city of the location page they have visited. However I am getting an error from WordPress that I cannot modify header information. I included the below function in my functions.php
file:
function set_city_cookie($city) {
if(!isset($_COOKIE['city_cookie'])) {
// set a cookie
setcookie('city_cookie', $city, time()+30);
$last_city=$_COOKIE['city_cookie'];
return $last_city;
}
}
add_action('init', 'set_city_cookie');
I then called the function in my single-locations.php
like this:
set_city_cookie($city);
Does anyone have any ideas as to why this wouldn't work?