I’ve used this and it works:
add_action('init', 'optin_cookie', 1);
function optin_cookie(){
$currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ( $currentURL == 'https://25dni.si/delovanje-uma/' ) :
setcookie( 'opt_in', has_opt_in, time()+31556926, "https://wordpress.stackexchange.com/");
elseif ( $currentURL != 'https://25dni.si/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
endif;
endif;
}
I’m sure there is a much more elegant way to do it, but this got it working for me …