admin管理员组文章数量:1202374
I am having an issue with setcookie. I need to set a cookie on a page action (form).
I have tried on the template file, but not working.
then find out if I set it from functions.php with init hook, it's working.
then I put condition like
if(is_page_template('mytemplate.php')){...
then it's not working.
how can I do that?
here is my code.
add_action( 'init', 'hello_my_cookie' );
function hello_my_cookie() {
if(is_page_template('mytemplate.php')){
if(isset($_POST['GGmyCookie'])){
setcookie( 'myCookie','Hello Cookie!', time()+3600);
}
}
}
I am having an issue with setcookie. I need to set a cookie on a page action (form).
I have tried on the template file, but not working.
then find out if I set it from functions.php with init hook, it's working.
then I put condition like
if(is_page_template('mytemplate.php')){...
then it's not working.
how can I do that?
here is my code.
add_action( 'init', 'hello_my_cookie' );
function hello_my_cookie() {
if(is_page_template('mytemplate.php')){
if(isset($_POST['GGmyCookie'])){
setcookie( 'myCookie','Hello Cookie!', time()+3600);
}
}
}
Share
Improve this question
asked May 31, 2022 at 21:31
Sarequl BasarSarequl Basar
236 bronze badges
0
1 Answer
Reset to default 0This fails because the init
hook is far too early to call is_page_template
. Conditionals such as this function use the main query to decide their value, but WordPress hasn't created the main query yet.
The earliest hook you can safely do this is template_redirect
.
本文标签: themessetcookie on WordPress Page Template
版权声明:本文标题:themes - setcookie on WordPress Page Template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738561426a2099310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论