How to Set, Get, and Delete WordPress Cookies?

Do you wish to learn about how to Set, Get, and Delete WordPress cookies? Cookies give you an elegant and simple solution to perform things like maintain sessions for your viewers as they browse your website, store the user’s preferences and gather data for your website. In this post, we will show you how to Set, Get, and Delete WordPress cookies.  

What is Cookies?

A cookie is a small piece of plain text files that are created and stored in the user’s web browser when they visit a website. The cookie contains all the information about the user such as the username/password, the items stored in their cart on the e-commerce websites etc.  

So again when the user visits your website, the cookie is sent back by the browser and it lets the website know the previous activity of users. Cookies are mostly encrypted files. The main aim of cookies is to assist the users. For example, when you shopping online, cookies will help the website to display the items that you are more likely to buy. Cookies play an important role in any website.  

Usages of Cookies on Different Websites

  • Can store the temporary session information when a user visits the website
  • Can store and manage users login details
  • For the E-commerce websites, cookies are used to remember the cart products when the user visits that site
  • Can track user activity on the website to offer them a personalized user experience and much more

How Cookies are Used in WordPress Website?

WordPress by default uses cookies to manage the user’s login sessions and their authentication. When the user comments on your website, by default the cookies will give the user name and email address. However, there is plenty of WordPress plugin on the website to Set their own cookies.

Also when you are going for third-party plugins like Google Adsense, Google Analytics and all these plugins will automatically Set the cookies on your website. To check your website cookies, navigate to browsers Settings >Advanced >Content Settings.

content

Under the content Settings, you will find the option “Cookies”. Click on the option and it will navigate to the cookies Setting page.

cookie

In the cookies, page click on “See all cookies and site data” option.  

see all cookies

Then in the next page, you will be able to see all the list cookies and site data which is stored on your browser by all the websites that you have visited.

all cookies

You can even type the website address in the search box if you are not able to view that website. Once you typed the website it will show all the data stored by that website.

search

By clicking on the single item you will Get more details about the individual cookies and their contents.

particular cookie

How to Set Cookies in WordPress?

In order to Set the cookies, you have to use the setcookies() function in PHP. This function will be able to accept the following parameters such as

  • Cookie name
  • Cookie value
  • Expire(optional: Sets a time period for the cookies)
  • Path(optional, But by default, it will use the root of the website)
  • Domain(optional, but by default will use the domain of your website)
  • Secure(optional, if true then only transfers cookie data via HTTPS)
  • HTTP only(optional, if it is Set true then the cookie will be accessible only via HTTP and cannot be used by scripts)

The syntax is

setcookie(name, value, expire, path, domain, secure, HTTP only);

Now you have the pass the values you want to store. For example, if you want to store the visitor’s username, then the code should be like the below one. Add the following lines of code in your WordPress themes functions.php file.

<?php

add_action(‘init’, ‘example_Setcookie’);

Function example_Setcookie() {

Setcookie($visitor_username, $username_value, 3* DAYS_IN_SECONDS, COOKIE_PATH,COOKIE_DOMAIN);

}

?>

 

You can notice that in the above code the time value is given for three days and after that, the cookie will expire. The DAYS_IN_SECONDS value is provided by WordPress which is a default. COOKIE_PATH specifies the path to your website and COOKIE_DOMAIN specifies your site domain.

You don’t have to worry about these parameters because they define on their own. If you are very efficient in working with PHP then you can Set the cookies as per the user input. Now when we run the function we will be able to see that the cookies are added to the browser. In case if you want to modify the cookie again, then you have to Set the cookie again using setcookie() function.

How to Get cookies in WordPress?

Once if at all cookies are created, you have to retrieve the data from them when your visitors return to your website. In order to prevent unwanted issues, you have to first use isSet() function to determine if the cookie has any value.

That is if the cookie was Set or not. If it was Set, then we can pass echo which the value to be retrieved. It is easy to retrieve the cookies in PHP using the $_COOKIE superglobal variable. So in order to Get the value of the cookie for what we created above, we have to locate it by name in the array.

 

<?php

if(!isSet($_COOKIE[$visitor_username]))

{

Echo “The cookie: ‘”. $visitor_username. “ ‘is not Set.”;

} else {

Echo  “The cookie: ‘”. $visitor_username. “ ‘is Set.”;

Echo “Value of cookie:”. $_COOKIE[$visitor_username];

}

?>

However, before passing the cookies name into the $_COOKIE variable, we must make sure that the cookie was Set. In the previous snippet, we did it by using the function isSet(). The isSet() function will return TRUE if the cookie is Set, if not it returns the value FALSE.

Note: Keep in my mind that if we Set a cookie and send it in the HTTP header, then the value is URL encoded automatically. And similarly when we retrieve the cookie, then the value is automatically decoded. In case if you want to avoid the URL encoding cookie then you can use the setrawcookie() function.

How to Delete cookies in WordPress?

We have successfully created and retrieved the cookies, now we want to know how to Delete them. The cookie manipulation process is very simple in WordPress.

Then syntax to Delete the cookie is

unSet($_COOKIE[Cookie_name]);

In order to Delete the cookie, we have to unSet the cookie and then use the same function as given in the Set-cookie.

 

<?php

unSet($_COOKIE[$visitor_username]);

Setcookie($visitor_username, ‘’, time()-(15*60));

?>

 

The first line of code is to remove the value of a cookie using the unSet() function from the $_COOKIE array. Then in the second line, we specify an empty value that forces the cookie to expire and passing in a timestamp that’s in the past.  

In the second line, the first parameter is the name of the cookie variable, the second parameter is null value and the third parameter denotes the time stamp (15*60) in the past. Once the cookie is Deleted, then you have to redirect your visitors to the WordPress home page. In order to do that add the following lines of code:

 

wp_redirect(home_url(), 302);

Exit;

Conclusion

Cookie manipulation in WordPress can be very easy if you know the basics of PHP. Even beginners will be able to work with it. Hope you understood on how to Set, Get, and Delete WordPress cookies. If you have any queries or suggestions, please feel free to comment to us. You can subscribe to us at Facebook and Twitter.

 

Leave a Comment

%d