WordPress插件缓存问题

I'm trying to set and read cookies within WordPress across my site by way of a custom plugin that I wrote.

add_action( 'wp_head', 'my_cookie_code' );

The code that I wrote works, however, once it's deployed to our live server it does not.

I think this is because of our WordPress caching system. When I turn it off, my cookie code works but once it's on, it does not.

Without turning off my WordPress caching system, how can I get my code to work with it?

I want each page to be cached by the caching system but I still want my little bit of code to be executed.

Thanks

It appears that this is an issue with the WPEngine caching system.

When I switched my code from being PHP based to JavaScript, I was able to solve the problem that I was having.

More information on this issue can be found here: Cookies and php sessions on WPEngine

<?php 
//Try this code in function.php file

//set cookie 
add_action( 'init', 'setCookie' );
function setCookie() {
setcookie( 'my-name', 'my-value', time() + 3600, COOKIEPATH, COOKIE_DOMAIN   );
}

//get Cookie 
add_action( 'wp_head', 'getCookie' );
function getCookie() {
$cookie_val = isset( $_COOKIE['my-name'] ) ? $_COOKIE['my-name'] : 'not set';
}

//Delete / Unset Cookie
add_action( 'init', 'unsetCookie' );
function unsetCookie() {
setcookie( 'my-name', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
}
?>

You need to contact WPE support and have them exclude those cookies specifically from Varnish caching on your install. Staging is uncached which would be why it works there.