• 欢迎来到本站,本站主要做程序员相关技术、博客、笔记,如果喜欢本网站那么赶紧使用Ctrl+D 收藏吧!
  • 如果有任何疑问,可以在下方文章下方留下你的评论~

[solved] How to set WordPress cookie?

视频教程 9个月前 (09-29) 2次浏览 0个评论

When using WordPress, cookies are often used to save the information of the website. For example, new and old visitors often need to display different welcome words when visiting the website. WordPress is a simple way to set a cookie.

Your Theme functions.php

function set_newuser_cookie() {
    //setting your cookies there
    if (!isset($_COOKIE['sitename_newvisitor'])) {
        setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}

add_action( 'init', 'set_newuser_cookie');

Example

When visitors visit your website, they can use the information of cookie to prompt different welcome words:

if (isset($_COOKIE['sitename_newvisitor'])) {//Old visitor
     echo 'welcome back!';
}
else {//New visitors
     echo 'Welcome to visit chinapenpal.com';
}

中国笔友, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:[solved] How to set WordPress cookie?
喜欢 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址