Need an SEO Expert? Get Your Free Strategy Session?
Skip to main content

managed services page speed

HOW TO SET, GET, DELETE COOKIE GUIDE FOR E-COMMERCE STORES

Est. reading time: 6 minutes

Quick Answer: How to Set, Get, and Delete WordPress Cookies.

How to Set, Get, and Delete WordPress Cookies.. Like a BOSS.

Cookies are small files that websites utilize to remember information about visitors. We utilize cookies on our own websites to make them more user-friendly and personalized.

numerous website owners want to utilize cookies but are unsure how to start. Cookies can assist you remember user preferences, keep people logged in, and even show targeted content.

In this ultimate guide, we will show you how to set, get, and delete WordPress cookies like a BOSS.
How to set, get, and delete cookies in WordPress.

What Are Cookies?

Cookies are plain text files that are created and stored in the users’ browsers when they visit a website. You can utilize cookies to add different features to your WordPress website.

Here are some common utilize cases for cookies:

Storing and managing a user’s login information
Storing temporary session information during a user’s visit
Remembering cart items during a user’s visit to an eCommerce store
Tracking user activity on a site to offer a personalized user experience

As you can see, cookies are a highly useful tool for website owners but 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 save and even share user activity across websites.

This is why the European Union enacted the EU Cookie Law, which requires website owners to declare that they utilize cookies to store information.

You can learn how to do this on your own site in our guide on how to add a cookies popup for GDPR/CCPA.
How Cookies Are Used on a Typical WordPress Website

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

However, numerous 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.

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

You can view all website cookies in your browser’s settings. For example, in Google Chrome, you need to start by opening the Settings page.

You can do this by clicking the ‘3 dots’ icon at the top right and selecting ‘Settings’ or by typing chrome://settings into the address bar.
Content settings in Google Chrome

On the Settings page, you need to search for ‘Content settings’.

Under ‘Content settings’, you will need to click on ‘Cookies’.
Cookies section in Chrome settings

This will open the cookies settings page.

Next, you need to click on the ‘See all cookies and site data’ option.
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 have visited.

You can type a website address in the search box, and you will be shown 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.
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 utilize a code snippet plugin such as WPCode. 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.

First, we will utilize the setcookie() function in PHP. This function accepts the following parameters:

Cookie name
Cookie value
Expire – optional and sets a time period after which the cookie expires
Path – optional and will utilize the site’s root by default
Domain – optional and uses your website’s domain by default
Secure – optional, and only transfers cookie data via HTTPS if true
httponly – optional, and when set to true, the cookie is only accessible via HTTP and cannot be used by scripts

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:

function wpb_cookies_tutorial1() {

$visit_time = date('F j, Y g:i a');

if(!isset($_COOKIE[wpb_visit_time])) {

setcookie('wpb_visit_time', $visit_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.
How to Get a Cookie and utilize It in WordPress

Now that we have created this cookie that’s stored in the user’s browser for one year, let’s look at how we can utilize this information on our website.

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:

function wpb_cookies_tutorial2() {

$visit_time = date('F j, Y g:i a');

if(isset($_COOKIE['wpb_visit_time'])) {

function visitor_greeting() {

$lastvisit = $_COOKIE['wpb_visit_time'];

$string .= 'You last visited our website '. $lastvisit .'. Check out whats new';

return $string;

}

} else {

function visitor_greeting() {

$string .= 'New here? Check out these resources...' ;

return $string;

}

setcookie('wpb_visit_time', $visit_time, time()+31556926);

}

add_shortcode('greet_me', 'visitor_greeting');

}

add_action('init', 'wpb_cookies_tutorial2');

We have commented within the code to show you what each part does. This code uses the information stored in the cookie and outputs it using shortcode.

You can now add the shortcode [greet_me] anywhere on your website, and it will show when a user last visited.

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.
Deleting a Cookie in WordPress

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

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


unset($_COOKIE['wpb_visit_time']);

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

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:

function wpb_cookies_tutorial2() {

$visit_time = date('F j, Y g:i a');

if(isset($_COOKIE['wpb_visit_time'])) {

function visitor_greeting() {

$lastvisit = $_COOKIE['wpb_visit_time'];

$string .= 'You last visited our website '. $lastvisit .'. Check out whats new';

unset($_COOKIE['wpb_visit_time']);

return $string;

}

} else {

function visitor_greeting() {

$string .= 'New here? Check out these resources...' ;

return $string;

}

}

add_shortcode('greet_me', 'visitor_greeting');

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.
Expert Guides on Using Cookies With WordPress

We hope this article helped you learn how to easily set, get, and delete WordPress cookies. You may also want to see some other guides related to using cookies in WordPress:

Glossary: Cookies
How to utilize Cookie Retargeting in WordPress to Show Custom On-Site Messages
How to Know if Your WordPress Website Uses Cookies
How to Add a Cookies Popup in WordPress for GDPR/CCPA
How to Clear Your Browser Cache in All Major Browsers (Fast Way)
How to Manage and Delete Transients in WordPress (The Easy Way)