How To Set/Create and Delete Cookies In WordPress? (A Complete Guide) Best of 2024

Are you facing a problem Set Cookies in WordPress. Cookies are a useful tool to store temporary information in the browser of users and are small file browsers used to save information about a user’s interaction with a website. Where then you can use this information to increase user experience through personalization and behavioral targeting.

As every website uses cookies for tracking visitors’ information when they are browsing pages or posts using their computers, telephones, tablets, etc. Cookies in WordPress offer a simple and beautiful solution to do things like maintain sessions for your visitors as they browse, save user preferences, and collect data for your site.

Unlike most modern web applications, WordPress is stateless. If you are going to create a web application at the top of WordPress, you will need some kind of system to keep the session. However, Cookies offer a simple, traditional mechanism for managing some settings for those users who have signed on to the front end.

Here, in this post, we are going to illustrate to you how to set, create and, delete cookies in WordPress.

First, we want to tell you some information about Cookies-

What Are Cookies & How to Use Them in a Typical WordPress Website?

The Cookies are the plain text files that are created and saved in the user’s browser when they visit a site. Cookies are used to add various features to the website.

Some of the common usage of cookies on different sites:

  • Save and Manage the user’s information.
  • Save temporary session information during users’ visits.
  • To remember the cart items during a user’s visit, e-commerce stores also use cookies.
  • Track the user activity on the website to provide a personalized user experience.

As you can see, cookies are very useful tools for site owners. But they can be a little offensive too. Recent trends in e-mail marketing, development hacking, and online marketing allow sites to set cookies in WordPress entirely, which act as a beacon and can be used to store and even share user activity across sites.

Thus, this is the reason that the European Union has enacted the EU Cookie Law, for which website owners need to declare that they use cookies to store information.

The Cookie Law is a piece of privacy legislation, that needs websites to save or retrieve any information on the premise that visitors have allowed their consent. And this legislation is approved by all EU member countries. It means that if your website is owned by the EU or targeted towards EU visitors you have to comply with it.

WordPress login user uses cookies to manage sessions and authentication. Also, it uses cookies to remember the name and email address of users if they fill out a comment form.

However, a lot of plugins on your site can set their own cookies in WordPress. Where a plugin is defined as a type of software that contains a group of functions that can be added to a WordPress website. They are used to provide additional functionality to your application.

For example- OptinMaster is a WordPress Lead Generation plugin. And it permits you to display different email option forms for new vs. returning visitors, and it does so by using cookies.

In case you are using third-party services like Google Analytics or Google AdSense on your website, they can also set cookies on your website.

Also, you can see all website cookies in your browser settings. For example, in Google Chrome, you do not need to go to Settings and search for ‘Content settings’.

chrome-settings (1)
cookies in WordPress


Under Content Settings, you have to click on the ‘Cookies’ to open the Cookies Settings page.

cookies
cookies in WordPress


After that, you have to click on ‘All Cookies and Site Data’ option.

seeallcookies
cookies in WordPress


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

Now, you can type a site address in the search box, and it will show you the data stored on that website.

Set, Get, and Delete Cookies in WordPress
cookies in WordPress


Clicking on an item will give you more information about personal cookies and their content.

Read Also: Ultimate Guide: How to Add a Cookies Popup on WordPress Website

Ways to Set, Get, and Delete Cookies in WordPress

Let’s get started!

How to Set Cookies in WordPress

You will have to add code to your function.php file of theme or a website-specific plugin. The function.php file is also known as the theme function file. It is used by the WordPress theme to define classes, actions, functions, and filters that are to be used by our templates. 

A theme is a group of stylesheets and templates that are used to describe the appearance and display of a WordPress site. A WordPress theme changes the layout & design of your site.

First, we will use the set cookie function in PHP. This function accepts the following parameters.

  • Cookie Value
  • Cookie name.
  • Path (Optional, by default, it will use the site’s root)
  • Expire (Optional: sets a time period after which cookie expires)
  • Secure (Optional, If true then transfers only cookie data through HTTPS)
  • Domain (Optional, by default, uses the domain of your website)
  • HTTP only (Optional, when set true the cookie is only accessible through HTTP and cannot be used by scripts)

Now add the code snippet to your WordPress website. This code saves the exact timestamp when a user visits your site in the 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);

}

}

Now you can visit your site and after that check your browser cookies. You will get a cookie with the name wpb_visit_time.

How to Get a Cookie and Use it in WordPress

Now that we’ve created this cookie that has been stored in a user’s browser for one year, see how we can use this information on our website.

If you know the name of the cookie, you can call it anywhere in PHP by using the $ _COOKIE [] variable.

Let’s add some code that not only sets cookies. But also uses it to do something on your site.

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’);

cookies in WordPress

However, we have commented on the code to display what each part does. Here, you can add shortcodes anywhere on your website. And it will be shown when a user last visited.

However, feel free to modify the code and to make it more useful for your site.

For example, you can display recent posts for returning users. And popular posts to new users. Where the articles posted by the admin of a WordPress site are called posts.

Deleting a Cookie in WordPress

Till now, we have learned how to set a cookie and use it later on your website.

Now we will show you how to delete a cookie-

To delete cookies in WordPress, you have to add the following line to your code.

unset($_COOKIE[‘wpb_visit_time’]);

Do not forget to replace wpb_visit_time with the cookie’s name that you want to delete.

Let’s put this code in the same context by using the same sample code that we used above. At this time we will delete a cookie and reset it with new information.

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’);
cookies in WordPress

You can see, this code deletes the cookies in WordPress after we have used the information stored inside. Later we set the cookie again with updated information.


Wrapping Up

In this post, we walk through a simple guide to set up, get, and delete cookies in WordPress by using PHP.

Often, we concluded its definition, how to use it, and how to set, get, and delete it.

We hope this blog helped you. If you have any suggestions or thoughts related to this, tell us in the comment section below! We are very happy to help you. Also, share the blog with your peers.

Thanks for visiting us. For more help on any other issues talk to our WordPress Support team to get instant advice & support, Dial +1-888-738-0846 (Toll-Free) to avail our WordPress security services.

thanks!


Leave a Reply