Cookies are actually a useful tool storing the temporary data in users’ browsers and then using it to improve user experience via personalization as well as behavioral targeting. For WordPress users, you can also use it on your WordPress websites. In the guide below, we are to introduce you the methods of setting, getting and deleting WordPress cookies. We hope you can learn from it and then take good advantage of cookies for your own site. (Note this guide requires you to have good knowledge of CSS, HTML, PHP as well as WordPress website, so you must be a professional for running a WordPress website.)
A cookie, is of course not a small sweet cake, but a piece of computer software enabling a website you have ever visited to recognize you when you visit it again. Also, cookies are those plain text files created and stored in users’ browsers as they visited a site. They can be used to add various features on your site. In below, we list some most common usages of cookies on different websites:
- Save and manage users’ login information;
- Save temporary session data when a user visits a site;
- For eCommerce stores, cookies are used to store the cart items when users visit them;
- Track user activities on your site so as to provide personalized user experience.
Looking at these usages, you can certainly find that cookies are highly useful tool for you running a website, especially for eCommerce websites. However, cookies are also a little invasive. For recent years, cookies are set in growth hacking, online marketing and email marketing to act as beacons and used to save as well as shared users’ activities across websites. Due to this, European Union took effect the EU Cookie Law that requires webmasters to announce that they utilize cookies to save information.
How Are Cookies Used in a Typical WordPress Site
Nevertheless, many plugins on WordPress website will set their own cookies. OptinMonster, for example, enables you to display various email option forms to the new and returning visitors, which is done well by utilizing cookies.
Besides, Google AdSense and Google Analytics are also likely to set cookies on your site if you are using them. You are able to check all cookies in the settings of your browsers. For instance, if you are using Google Chrome, then you need to find “Settings” and here find “Content settings” to check all website cookies.
Under the “Content settings” menu, you need to choose “Cookies” to open the “Cookies settings” page.
Then, you will need to select “All cookies and site data”.
Now, there will be the page showing a list of all the cookies as well as website data saved in your browser by the websites you have ever visited. If you want to check the specific websites’ data, you can just enter the website address in the search box on the top right corner.
You can also choose one single item of the cookies list, and then it will display your more about each cookie and its content.
How to Set a Cookie in Your WordPress Site?
In order to follow this guide, you are to add code snippet to your funtions.php file of your WordPress theme or other site-specific extension or plugin.
At first, you will need to make full use of PHP “setcoookie()” function which can receive the parameter as following:
- Cookie value
- Cookie name
- Expire: this is optional to set a time period after a cookie expires.
- Path: this is optional, too, to use a site’s root by default.
- Domain: another optional parameter to use a site’s domain by default.
- Secure: it is optional. If it is true, then the site only allows transferring cookies data through HTTPS.
- Httponly is optional as well. If the setting of the parameter is true, then the cookie can be accessible through HTTP only but can’t be utilized by scripts.
After showing you the PHP setcookie() function’s parameter, we should move to add the code snippet to your site. The code will save the true timestamp when any user visited your site via a cookie.
At present, you can visit your site and look up your browser cookies. Then, you can find a cookies named as wpb_visit_time.
How to Get and Use a Cookie in Your WordPress Site?
Currently, we have created a cookie storied in your browser for one year. Then, we can have a look at how to use the information save by the cookies on your site.
It is simple. If you know a cookie’s name, then you can call it everywhere easily in PHP by utilizing $_COOOKIE variable. Now, you will need to add some code snippet which can set your cookie and use the cookie to finish other things on your WordPress site.
We have stated that the code snippet to display you what those parts do. The code takes advantage of the cookie’s information and then outputs it by utilizing shortcodes. At present, you are able to add the shortcode “[greet_me]” in any pace of your site, and then it will display when a user visited last time.
You can modify this code as you want to better fit your website’s needs. For example, the code can be used to display returning users those recent posts and new users the popular posts.
How to Delete a Cookie in Your WordPress Site?
By far, we have shown you how to set and use a cookie in your WordPress website. Now, we should start telling you how to delete these coolies in your site. To make it, you will need to add a command line into your code, and the line is just like the following:
Never forget to use the cookie name you want to delete to replace the “wpb_visit_time”.
Now, you can put this modified code in context by utilizing the same code used above. Here we are to delete a cookie and then set it again with fresh data.
Checking the code above, you can see we have deleted the cookie after we used its information. According to the code, we also set it again with new time information.
So far, we have introduced how to set, get, use and delete a cookie in your WordPress website. It is not very hard, so we hope you can learn it and use it to improve your user experience by following this guide step by step. If you have more issues about WordPress unfixed, you can check out our WordPress Tutorial to search for answers.