How to Manage WordPress Cookies Like a Pro


Cookies are tools that are used to save temporary information on the user’s browser and then, later on, use it to enhance the user experience by behavioral targeting. If you are here, then you must be keen to know how to use cookies in your WordPress site.

Knowing About Cookies

When a user visits a website then some plain text files are created and stored in the user’s browser. These files are cookies. These files help in adding different features to the website.Manage-WordPress-Cookies-Cover-Image

Cookies store and manage user login information, store temporary session information during a users visit, remember cart items in e-commerce stores, track user’s activity to offer a personalized experience and the list goes on.

How cookies are used on WordPress websites

WordPress remembers logged-in user session and authentication with the help of cookies. If any user fills a comment form then WordPress uses cookies to remember their user-name and email.

Some third-party services, used on your website, also set cookies on your website. Take Google AdSense for example.

Also, some plugins used on your website also set their own cookies.



How to set a cookie in WordPress

All you need to do is add the following code snippet in your WordPress site. This code is used for storing a user’s activities, when he/she visits your site, in the form of a cookie.

1
2
3
4
5
6
7
8
9
10
11
12
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('hrd_visit_time', $current_time, time()+31556926);
}
}

To confirm, you can check your browser cookies where you will see a cookie named hrd_visit_time.

How to get and then use the cookie in WordPress

After creating the cookie we need to use its information on our website. So let us do the same with the cookie that we created above.

You can call your cookie anywhere in PHP using the code $_COOKIE[] variable if you remember the name of the cookie. Now add the following code which sets the cookie and also uses it to do something on your website.

What this code does is that it uses the information stored in the cookie and outputs it using the shortcode. To know when a user last visited add the code [greet_me]  anywhere on your website.

Deleting a cookie

Add the following line to your code, to delete a cookie.

1
unset($_COOKIE['hrd_visit_time']);

Replace wpb_visit_time with the name of the cookie that you wish to delete.

Using the sample code that has been used above, let us put this code in some context. This code will delete a cookie and then set it again with new information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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');

If you try to understand once we used the information inside a cookie, this code deletes it. Then with updated time, we set the cookie again.

That’s all. Now you are good to manage your WordPress cookie yourself.

We hope our article helped you:).

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.