简体   繁体   English

如何在WordPress中设置cookie?

[英]How to set cookie in wordpress?

I want to set cookie in wprdpress but its not working.This is my code 我想在wprdpress中设置cookie但它不起作用。这是我的代码

add_action('init', 'set_new_cookie');
    function set_new_cookie() {
        setcookie('cart_item','some value', time() + (86400 * 30), "/");
    }

but this is not working. 但这不起作用。 Actually init hook is not working.I am trying to do ajax request here(page visitors are not logged in). 实际上init钩子不起作用。我正在尝试在这里执行ajax请求(页面访问者未登录)。

setcookie('cart_item',$carts,time() + (86400*30),'/');

Its working fine for me. 它对我来说很好。 / at the last is to set cookie for whole site. /最后是为整个网站设置Cookie。 Remove / to set cookie for particulat directory only. 删除/,仅将cookie设置为particulat目录。

Well if this is the sequence your code runs then you need to switch the function and the call to it. 好吧,如果这是您的代码运行的顺序,那么您需要切换函数并对其进行调用。 Now you are calling the function before you initiated it. 现在,您在启动该函数之前先对其进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM