Hi I am new to wordpress.
I had researched lot about this but didn’t succeed. I want to set cookie value NULL before logout and custom value after admin logged in.
So I put below code in wp-includes/puggable.php => in the function wp_set_auth_cookie
setcookie("cookieName", '', time() + (1 * 60) , true);
echo "<script>console.log('Cookie must set null')</script>";
But didn’t get succeed cookie value remain same even after login-logout.
NEVER edit WordPress core files! If you update WordPress one day (and you’ll want to, as to fix the security issues), all your modifications will be lost.
What you need to do is to work with the
wp_login
andwp_logout
actions. You attach on both of those actions functions withadd_action
, so they will be triggered on those events. Those functions and theadd_action
calls take place inside the functions.php file of your theme.So inside functions.php, add this to set the cookie on login (for admin only):
Note that as diziaq pointed out in his answer, you was using wrong the
setcookie
function.And to delete the cookie on log out:
Same as diziaq pointed out, you need to remove the cookie – you can’t set it to
NULL
.Please read docs for
setcookie
function – you use it wrong. The fourth parameter must be astring
(but you’re passing abool
).To set a cookie:
setcookie("cookieName", "cookieValue", time() + 600, '/');
To delete the cookie you have to set expiration date in the past.
setcookie("cookieName", "anyValue", time() - 1, '/');
Your intention to set
null
to a cookie is unreachable, because by definition the value of a cookie is a string. It can be empty, but it cannot be undefined or null.