wordpress获取封面_如何设置,获取和删除WordPress Cookies(如Pro)

wordpress获取封面

Do you want to learn how to use cookies on your WordPress site? Cookies are a useful tool to store temporary information in user’s browser and then use this information to enhance user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPress cookies like a pro.

您是否想学习如何在WordPress网站上使用Cookie? Cookies是一种有用的工具,用于将临时信息存储在用户的浏览器中,然后使用此信息通过个性化和行为定向来增强用户体验。 在本最终指南中,我们将向您展示如何像pro一样设置,获取和删除WordPress cookie。

How to set, get, and delete cookies in WordPress

Note: 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?)

Cookies are plain text files that are created and stored in users browser when they visit a website. Cookies are used to add different features on a website.

Cookies是纯文本文件,它们是在用户访问网站时创建并存储在用户浏览器中的。 Cookies用于在网站上添加不同的功能。

Following are some of the common usage of cookies on various websites.

以下是各种网站上Cookie的一些常见用法。

  • Store and manage user’s login information

    存储和管理用户的登录信息
  • Store temporary session information during a users visit

    在用户访问期间存储临时会话信息
  • Ecommerce stores 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 email marketing, growth hacking, 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 EU Cookie Law, 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)

By default, WordPress uses cookies to manage logged-in user sessions and authentication. It also uses cookies to remember a user’s name and email address if they fill out a comment form.

默认情况下,WordPress使用cookie来管理登录的用户会话和身份验证。 如果用户填写了评论表格,它还会使用Cookie记住用户的姓名和电子邮件地址。

However, many WordPress plugins on your website may also set their own cookies. For example, OptinMonster allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.

但是,您网站上的许多WordPress插件也可能会设置自己的Cookie。 例如, OptinMonster允许您向新访客和回访者显示不同的电子邮件选择形式,并且使用Cookie来做到这一点。

If you are using third party services on your website like Google Analytics or Google AdSense, then they may also set cookies on your website.

如果您在网站上使用诸如Google Analytics(分析)Google AdSense之类的第三方服务,那么它们也可能在您的网站上设置cookie。

You can view all website cookies in your browser’s settings. For example, in Google Chrome you need to go to settings and search for ‘content settings’.

您可以在浏览器的设置中查看所有网站cookie。 例如,在Google Chrome浏览器中,您需要转到设置并搜索“内容设置”。

Content settings in Google Chrome

Under content settings, you will need to click on ‘Cookies’ to open the cookies settings page.

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

Cookies section in Chrome settings

Next, you need to click on the ‘All cookies and site data’ option.

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

View all cookies and site data

On the next page, you will see a list of all cookies and site data stored on your browser by all websites you visited.

在下一页上,您将看到访问的所有网站存储在浏览器中的所有cookie和站点数据的列表。

You can type a website address in the search box, and it will show you the data stored by that website.

您可以在搜索框中输入网站地址,它会向您显示该网站存储的数据。

View site cookies

Clicking on a single item will show you more details about individual cookies and their contents.

单击单个项目将为您显示有关单个cookie及其内容的更多详细信息。

如何在WordPress中设置Cookie (How to Set a Cookie in WordPress)

To follow this tutorial, you will need to add code to your theme’s functions.php file or a site-specific plugin. If you haven’t done this before, then please take a look at our guide on how to copy and paste code snippets in WordPress.

要遵循本教程,您需要将代码添加到主题的functions.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,而脚本不能使用它)

Now let’s add a code snippet to your WordPress site. This code stores the exact timestamp when a user visited your website in a 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)

Now that we have created this cookie that’s stored in user’s browser for 1 year, let’s take a look at how can we use this information on our website.

现在,我们已经创建了该cookie,并将其存储在用户的浏览器中一年了,让我们看看如何在我们的网站上使用此信息。

If you know the name of a cookie, then you can easily call it anywhere in PHP using the $_COOKIE[] variable. Let’s add some code that not only sets the cookie but also uses it to do something on your website.

如果您知道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');

We have commented the code to show you what each part does. This code uses the information stored in the cookie and outputs it using the shortcode. You can now add shortcode [greet_me] anywhere on your website, and it will show when a user last visited.

我们对代码进行了注释,以向您展示每个部分的功能。 该代码使用存储在cookie中的信息,并使用简码输出该信息。 现在,您可以在网站上的任何位置添加简码[greet_me],它将显示用户上次访问的时间。

Feel free to modify the code to make it more useful for your website. For example, you can show recent posts to returning users and popular posts to new users.

随意修改代码,使其对您的网站更有用。 例如,您可以向回头用户显示最近的帖子 ,向新用户显示 热门帖子

在WordPress中删除Cookie (Deleting a Cookie in WordPress)

So far we have learned how to set a cookie and use it later in your website. Now let’s take a look at how to delete a cookie.

到目前为止,我们已经学习了如何设置cookie并稍后在您的网站中使用它。 现在让我们看一下如何删除cookie。

To delete a cookie, you need to add the following line to your code.

要删除Cookie,您需要在代码中添加以下行。


unset($_COOKIE['wpb_visit_time']);

Don’t forget to replace wpb_visit_time with the name of the cookie you are trying to delete.

不要忘记将wpb_visit_time替换为您要删除的Cookie的名称。

Let’s put this code in some context using the same sample code we used above. This time we will delete a cookie and set it again with new information.

让我们使用上面使用的相同示例代码将此代码放在某些上下文中。 这次,我们将删除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');


As you can see, this code deletes the cookie once we have used the information stored inside. Later we set the cookie again with the updated time information.

如您所见,一旦我们使用了存储在其中的信息,此代码将删除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 extremely useful tricks for the WordPress functions file.

我们希望本文能帮助您学习如何轻松设置,获取和删除WordPress cookie。 您可能还想查看我们有关WordPress函数文件的其他非常有用的技巧的列表。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/

wordpress获取封面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值