Not running the init event in WordPress when the page loads, what could be the reason?

Good afternoon. There was a following problem:
There is a simple plugin:
shorcode_func function( $atts ) {
 $a = shortcode_atts( array(
 'landing' => ",
 ), $atts );

if(isset($_COOKIE['pid'])){
 $pid = "?pid=".$_COOKIE['pid'];
}else{
 $pid = "";
}

 return $a['landing'].$pid;
}
is the add_shortcode( 'landinglink', 'shorcode_func' );

add_action( 'init', 'pid_to_cookie');

pid_to_cookie function(){
if(!isset($_COOKIE['pid'])){
if(isset($_GET['pid'])){
 setcookie ("pid", $_GET['pid']);
}
}
}


But it works only on the main page. Catches GET only on the homepage, the shortcode is working fine. Scratching their heads can't understand the reason for this miracle...
Thank you.
July 8th 19 at 16:10
1 answer
July 8th 19 at 16:12
Solution
Solved the problem this way:
add_action( 'init', 'pid_to_cookie');
pid_to_cookie function(){
if(isset($_GET['pid'])){
 setcookie('pid', $_GET['pid'], time()+1209600, '/', 'site.ru');
}

}

Find more questions by tags WordPress