0

I have this code to set cookie when user on the taxonomy archive page called 'term-country'

if (is_tax('term-country'))
add_action('init', 'my_setcookie');

// my_setcookie() set the cookie on the domain and directory WP is installed on
function my_setcookie(){
    $path = parse_url(get_option('siteurl'), PHP_URL_PATH);
     $host = parse_url(get_option('siteurl'), PHP_URL_HOST);
     $expiry = strtotime('+1 month');
     setcookie('location', 'my_cookie_value_3', $expiry, $path, $host);
}

when I add the is_tax() function the code will not work when I remove the is_tax() function the code will work

is there any issues with my code ?

  • Have you tried any other methods? Like is_archive(). – ngearing Nov 16 '15 at 22:21
  • is_tax won't work before wp action, the query has to run before conditionals will work. – Milo Nov 16 '15 at 22:52
  • @Nath no I did not. – Abdullah Alsigar Nov 17 '15 at 1:57
  • @Milo can I still use is_tax function and then instead of using add_action I will use jquery to set the cookie ? – Abdullah Alsigar Nov 17 '15 at 1:58
-1

After googling around I found the correct way to do it by using the 'wp' hook

if (is_tax('term-country'))

add_action('wp', 'my_setcookie'); // my_setcookie() set the cookie on the domain and directory WP is installed on function my_setcookie(){ $path = parse_url(get_option('siteurl'), PHP_URL_PATH); $host = parse_url(get_option('siteurl'), PHP_URL_HOST); $expiry = strtotime('+1 month'); setcookie('location', 'my_cookie_value_3', $expiry, $path, $host); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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