Setting cookies in WordPress

I am trying to set a cookie on my site and wanna switch the style sheets using this cookie.

I’ve tested a normal function but it does not work.
I’ve put below code into my functions.php

add_action('init', 'cookiecheck');

function cookiecheck(){         
    setcookie( 'cookiename', 'cookievalue', time() + 3600, 'mysite.net/sites/site1/', 'mysite.net/sites/site1/');       
}       

After I add this code if i check my cookies tab using firefox developer tools I don’t see a cookie called cookiecheck

Why is this?
How to set a cookie in a wordpress site?

1 Answer
1

Change this line

setcookie( 'cookiename', 'cookievalue', time() + 3600, 'mysite.net/sites/site1/', 'mysite.net/sites/site1/');

to

setcookie( 'cookiename', cookievalue, time() + 3600, COOKIEPATH, COOKIE_DOMAIN);`

COOKIEPATH and COOKIE_DOMAIN are WP constants now, but the codex is not updated yet.

Leave a Comment