
WordPress 获得掩护
您想了解如何在 WordPress 网站上使用 cookie 吗? Cookie 是一种有用的工具,可在用户浏览器中存储临时信息,然后使用此信息通过个性化和行为定位来增强用户体验。在本终极指南中,我们将向您展示如何像专业人士一样设置、获取和删除 WordPress cookie。
您想了解如何在 WordPress 网站上使用 cookie 吗? Cookie 是一种有用的工具,用于在用户的浏览器中存储临时信息,然后使用此信息通过个性化和行为定位来增强用户体验。在本终极指南中,我们将向您展示如何像专业人士一样设置、获取和删除 WordPress cookie。

笔记: This is an advanced tutorial. It requires you to have proficient understanding of HTML, CSS, WordPress site, and PHP.
注意:这是高级教程。 它要求您精通HTML,CSS,WordPress网站和PHP。
什么是饼干? (What Are Cookies?)
Cookie 是用户访问网站时创建并存储在用户浏览器中的纯文本文件。 Cookie 用于在网站上添加不同的功能。
Cookie 是用户访问网站时创建并存储在用户浏览器中的纯文本文件。 Cookie 用于向网站添加不同的功能。
以下是各种网站上 cookie 的一些常见用法。
以下是各种网站上 cookie 的一些常见用途。
- Store and manage user’s login information 存储和管理用户的登录信息
- Store temporary session information during a users visit 在用户访问期间存储临时会话信息
- 电子商务商店 use cookies to remember cart items during a user’s visit电子商务商店使用Cookie来记住用户访问期间的购物车商品
- Track user activity on a site to offer personalized user experience 跟踪网站上的用户活动以提供个性化的用户体验
- and more 和更多
As you can see, cookies are highly useful tool for website owners, but they can also be a bit invasive. Recent trends in 电子邮件营销, 增长黑客, and online marketing as a whole allow websites to set cookies that act as a beacon and can be used to store and even share user activity across websites.
如您所见,Cookie对网站所有者而言是非常有用的工具,但它们也可能具有侵入性。 电子邮件营销 , 增长黑客和在线营销整体上的最新趋势使网站能够设置充当信标的cookie,并可以用来存储甚至在网站之间共享用户活动。
This is why European Union enacted the 欧盟 Cookie 法, which requires website owners to declare that they use cookies to store information.
这就是欧盟颁布《 欧盟 Cookie 法的原因 ,该法律要求网站所有者声明他们使用Cookie来存储信息。
在典型的WordPress网站中如何使用Cookies (How Cookies are Used in a Typical WordPress Website)
默认情况下,WordPress 使用 cookie 来管理登录的用户会话和身份验证。如果用户填写了评论表,它还会使用 cookie 来记住用户的姓名和电子邮件地址。
默认情况下,WordPress 使用 cookie 来管理登录的用户会话和身份验证。如果用户填写评论表,它还会使用 cookie 来记住用户的姓名和电子邮件地址。
However, many WordPress 插件 on your website may also set their own cookies. For example, 选择怪物 allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.
但是,您网站上的许多WordPress 插件也可能会设置自己的Cookie。 例如, 选择怪物允许您向新访客和回访者显示不同的电子邮件选择形式,并且使用Cookie来做到这一点。
If you are using third party services on your website like 谷歌分析 or 谷歌的AdSense, then they may also set cookies on your website.
如果您在网站上使用诸如谷歌分析或谷歌的AdSense类的第三方服务,那么它们也可能在您的网站上设置cookie。
您可以在浏览器设置中查看所有网站 cookie。例如,在 Google Chrome 中,您需要转到设置并搜索“内容设置”。
您可以在浏览器设置中查看所有网站 cookie。例如,在 Google Chrome 中,您需要转到设置并搜索“内容设置”。

在内容设置下,您需要单击“Cookie”以打开 Cookie 设置页面。
在内容设置下,您需要点击“Cookies”才能打开cookie设置页面。

接下来,您需要单击“所有 cookie 和站点数据”选项。
接下来,您需要单击“所有 cookie 和站点数据”选项。

在下一页上,您将看到您访问过的所有网站存储在浏览器上的所有 cookie 和网站数据的列表。
在下一页上,您将看到您访问的所有网站的浏览器中存储的所有 cookie 和网站数据的列表。
您可以在搜索框中输入网站地址,它会显示该网站存储的数据。
您可以在搜索框中输入网站地址,它将显示该网站存储的数据。

单击单个项目将向您显示有关各个 cookie 及其内容的更多详细信息。
单击单个项目将向您显示有关单个 cookie 及其内容的更多详细信息。
如何在WordPress中设置Cookie (How to Set a Cookie in WordPress)
To follow this tutorial, you will need to add code to your theme’s 函数.php file or a 特定于站点的插件. If you haven’t done this before, then please take a look at our guide on 如何在 WordPress 中复制和粘贴代码片段.
要遵循本教程,您需要将代码添加到主题的函数.php文件或具体的于在网站的插件中 。 如果您之前没有做过,请查看我们的指南, 了解如何在 WordPress 中复制和粘贴代码片段 。
First we will use the setcookie()
function in PHP. This function accepts the following parameters.
首先,我们将在PHP使用setcookie()
函数。 该函数接受以下参数。
- Cookie name Cookie名称
- Cookie value Cookie值
- Expire (Optional: sets a time period after which cookie expires) 过期(可选:设置Cookie过期的时间段)
- Path (Optional, by default it will use the site’s root) 路径(可选,默认情况下,它将使用网站的根目录)
- Domain (Optional, by default uses your website’s domain) 域(可选,默认情况下使用您网站的域)
- Secure (Optional, If true then only transfers cookie data via HTTPS) 安全(可选,如果为true,则仅通过HTTPS传输cookie数据)
- httponly (Optional, when set true the cookie is only accessible via HTTP and cannot be used by scripts) httponly(可选,当设置为true时,只能通过HTTP访问cookie,而脚本不能使用它)
现在让我们向您的 WordPress 网站添加一个代码片段。此代码在 cookie 中存储用户访问您网站时的准确时间戳。
现在,让我们将代码片段添加到您的 WordPress 网站。此代码存储用户访问 cookie 时的准确时间戳。
function wpb_cookies_tutorial1() {
$visit_time = date('F j, Y g:i a');
if(!isset($_COOKIE[$wpb_visit_time])) {
// set a cookie for 1 year
setcookie('wpb_visit_time', $current_time, time()+31556926);
}
}
You can now visit your website and then check your browser cookies. You will find a cookie with the name wpb_visit_time
.
您现在可以访问您的网站,然后检查您的浏览器cookie。 您会发现一个名为wpb_visit_time
的cookie。
如何获取Cookie并在WordPress中使用它 (How to Get a Cookie and Use it in WordPress)
现在我们已经创建了这个 cookie,并在用户的浏览器中存储了 1 年,让我们看看如何在我们的网站上使用这些信息。
现在我们已经创建了该 cookie 并将其在用户的浏览器中存储了一年,让我们看看如何在我们的网站上使用此信息。
如果您知道 cookie 的名称,那么您可以使用 $_COOKIE[] 变量轻松地在 PHP 中的任何位置调用它。让我们添加一些代码,不仅可以设置 cookie,还可以使用它在您的网站上执行某些操作。
如果您知道 cookie 的名称,则可以使用 $_COOKIE[] 变量轻松地在 PHP 中的任何位置调用它。让我们添加一些代码,不仅可以设置 cookie,还可以使用它在您的网站上执行某些操作。
function wpb_cookies_tutorial2() {
// Time of user's visit
$visit_time = date('F j, Y g:i a');
// Check if cookie is already set
if(isset($_COOKIE['wpb_visit_time'])) {
// Do this if cookie is set
function visitor_greeting() {
// Use information stored in the cookie
$lastvisit = $_COOKIE['wpb_visit_time'];
$string .= 'You last visited our website '. $lastvisit .'. Check out whats new';
return $string;
}
} else {
// Do this if the cookie doesn't exist
function visitor_greeting() {
$string .= 'New here? Check out these resources...' ;
return $string;
}
// Set the cookie
setcookie('wpb_visit_time', $visit_time, time()+31556926);
}
// Add a shortcode
add_shortcode('greet_me', 'visitor_greeting');
}
add_action('init', 'wpb_cookies_tutorial2');
我们对代码进行了注释,以向您展示每个部分的作用。此代码使用 cookie 中存储的信息并使用短代码输出。您现在可以在网站上的任何位置添加短代码 [greet_me],它将显示用户上次访问的时间。
我们对代码进行了注释,以向您展示每个部分的作用。此代码使用 cookie 中存储的信息并使用短代码输出该信息。现在,您可以在网站的任何位置添加短代码 [greet_me],它将显示用户上次访问的时间。
Feel free to modify the code to make it more useful for your website. For example, you can 显示最近的帖子 to returning users and 热门帖子 to new users.
随意修改代码,使其对您的网站更有用。 例如,您可以向回头用户显示最近的帖子 ,向新用户展示 热门帖子 。
在WordPress中删除Cookie (Deleting a Cookie in WordPress)
到目前为止,我们已经了解了如何设置 cookie 并稍后在您的网站中使用它。现在我们来看看如何删除cookie。
到目前为止,我们已经了解了如何设置 cookie 并稍后在您的网站中使用它。现在让我们看看如何删除cookie。
要删除 cookie,您需要将以下行添加到代码中。
要删除 cookie,您需要将以下行添加到您的代码中。
unset($_COOKIE['wpb_visit_time']);
不要忘记将 wpb_visit_time 替换为您要删除的 cookie 的名称。
不要忘记将 wpb_visit_time 替换为您要删除的 cookie 的名称。
让我们使用上面使用的相同示例代码将此代码放在某些上下文中。这次我们将删除 cookie 并使用新信息重新设置它。
让我们使用上面使用的相同示例代码将此代码放入某些上下文中。这次,我们将删除 cookie 并使用新信息重新设置它。
function wpb_cookies_tutorial2() {
// Time of user's visit
$visit_time = date('F j, Y g:i a');
// Check if cookie is already set
if(isset($_COOKIE['wpb_visit_time'])) {
// Do this if cookie is set
function visitor_greeting() {
// Use information stored in the cookie
$lastvisit = $_COOKIE['wpb_visit_time'];
$string .= 'You last visited our website '. $lastvisit .'. Check out whats new';
// Delete the old cookie so that we can set it again with updated time
unset($_COOKIE['wpb_visit_time']);
return $string;
}
} else {
// Do this if the cookie doesn't exist
function visitor_greeting() {
$string .= 'New here? Check out these resources...' ;
return $string;
}
}
add_shortcode('greet_me', 'visitor_greeting');
// Set or Reset the cookie
setcookie('wpb_visit_time', $visit_time, time()+31556926);
}
add_action('init', 'wpb_cookies_tutorial2');
正如您所看到的,一旦我们使用了其中存储的信息,此代码就会删除 cookie。稍后我们使用更新的时间信息再次设置 cookie。
正如您所看到的,一旦我们使用了 cookie 中存储的信息,此代码就会删除 cookie。随后,我们使用更新的时间信息再次设置 cookie。
We hope this article helped you learn how to easily set, get, and delete WordPress cookies. You may also want to see our list of other WordPress 函数文件非常有用的技巧.
我们希望本文能帮助您学习如何轻松设置,获取和删除WordPress cookie。 您可能还想查看我们关于 WordPress 功能文件的其他非常有用的提示列表。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/
WordPress 获得掩护