For the record, setting cookies in WordPress is special.
I've been stuck on this little problem for a day.
Setting cookies in WordPress is special.It should be written in the theme's functions file and loaded into the init hook.
function custom_set_cookie() { setcookie( 'key', 'value', time() + 3600 * 24, COOKIEPATH, COOKIE_DOMAIN ); } add_action( 'init', 'custom_set_cookie' );