我正在尝试使用functions.php中的钩子在wordpress中设置一个cookie,但是cookie值是空的。如何设置从 URL ($_GET) 获取值的 cookie?
(编辑)我只需要将此 cookie 设置在特定页面上,因此我将其包装在 if 语句中。cookie 现在设置正确。但是当我移动到另一个页面并尝试获取 cookie 时,它不再设置了!任何想法为什么会发生这种情况?
(编辑 2)我通过将 cookie 路径设置为“/”来解决此问题。
function set_cookie(){
if(is_page('hotel')){
$destinationType = 'city'; //'city';
$destinationCode = $_GET["dcode"]; //'lon';
$checkinDate = $_GET["date_in"]; //date('2013-08-05'); // '2006-05-10';
$checkoutDate = $_GET["date_out"];
$date1 = strtotime($checkinDate);
$date2 = strtotime($checkoutDate);
$datediff = $date2 - $date1;
$duration = floor($datediff / (60 * 60 * 24));
//Create Search Object
$search = array();
$search[0] = $destinationType;
$search[1] = $destinationCode;
$search[2] = $checkinDate;
$search[3] = $checkoutDate;
$search[4] = $duration;
//Create Rooms Object
$packages = array();
$npack = intval($_GET['numpack']);
for ($j = 0; $j < $npack; $j++) {
$packages[$j][0] = $_GET['rtype' . ($j + 1)];
$packages[$j][1] = 1; //$_GET['nroom' . ($j + 1)];
$packages[$j][2] = $_GET['cot' . ($j + 1)];
if ($packages[$j][2] == null) {
$packages[$j][2] = '0';
}
for ($k = 0; $k < intval($_GET['children' . ($j + 1)]); $k++) {
$packages[$j][3][$k] = $_GET['achild' . ($j + 1) . '_' . ($k + 1)];
}
}
//Complete Search Object to cookie
$searchObject = array($search, $packages);
setcookie('searchobject', json_encode($searchObject), time() + 3600, "/");
}
}
add_action('get_header', 'set_cookie');