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.
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
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.
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.
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.
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:).