Guide: How to Set, Get, and Delete Cookies in WordPress?

Guide: How to Set, Get, and Delete Cookies in WordPress?

Are you facing problem to Set, Get, and Delete Cookies in WordPress. Cookies are a useful tool to store temporary information in the browser of users. And then you can use this information to increase user experience through personalization and behavioral targeting.

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 you how to set, get and, delete cookies in WordPress.

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

What Are Cookies?

The Cookies are the plain text files that 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’ visit.
  • To remember the cart items during a user’s visit, e-commerce stores also use the cookies.
  • Track the user activity on the website to provide a personalized user experience.

As you can see that, 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 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 the information.

How to use cookies in a typical WordPress website

WordPress login user uses cookies to manage sessions and authentication. Also, it uses the 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. 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)
Under Content Settings, you have to click on the ‘Cookies’ to open the Cookies Settings page.

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

seeallcookies
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
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

Way to Set, Get, and delete Cookies in WordPress

Let’s get started!

How to Set a Cookie in WordPress

You will have to add code to your function.php file of theme or a website-specific plugin.

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

However, we have commented on the code to display you that what each part does. Here, you can add shortcode anywhere to 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.

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 a cookie, 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 some context by using 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’);

You can see, this code deletes the cookie 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, get and delete cookies in WordPress by using the 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 suggestion 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 844 275 0975 (Toll-Free) to avail our WordPress security services.

Read More Blogs:



Leave a Reply

Call Now