Are you looking to learn how to manage your WordPress cookies? No, I do not mean the cookies that you eat. Cookies store temporary information in the visitor’s browser and then they are called upon to improve the experience. Cookies are responsible for some personalization and behavioral targeting aspects on the website.
What Exactly is a Cookie?
Most people will immediately think of their favorite type of cookie, but again that is the wrong kind of cookie.
The cookies web developers talk about are plain text files stored inside of a visitor’s browser. They have multiple uses that make them an essential part of any website. Here are some of the most important things cookies are responsible for:
- Store temporary information during your user’s visits
- Store and manage the login information on your website
- Track and personalize user activity
Cookies are extremely useful, but at the same time, they can be harmful to users. These cookies store important information and they can be used maliciously. As a result, there are many internet regulations that have been put in place this year and one of them is the EU Cookie Law.
It is a very simple law that requires a website to disclose that cookies are being used if your website is accessed in the EU. Thus, if you get visitors from the EU, you need to make sure you give a warning.
How Do I Check What Cookies My Website Is Using
Since cookies are stored in your browser, you can use your browser to check for them. The method is different depending on the browser you use. In this example, I will use the world’s most popular web browser, Google Chrome.
Keep in mind you will see cookies from every website. Thus, to ensure you only see the cookies that your website uses, you should clear the cookies of your browser first. Then simply visit your website for your browser to acquire the cookies it uses.
Click on the Settings option on your browser.
Type «Content Settings» into the text box at the top of the screen. Click on the Content Settings option.
The first option you should see is Cookies. Click on the Cookies option and select the See all cookies and site data option.
You will now see a full list of cookies that your browser has stored. You can expand any of them to learn what they do. It is a good idea to regularly clear your browser’s cookies. However, remember they control those very convenient automatic login fields.
How to Manage Cookies in WordPress
Today, I will demonstrate how to manage cookies in WordPress. Cookies are not easy to understand if you do not have a coding background. There are three things to do with cookies. Get, set, and delete. I will demonstrate how to go through each of these. Remember, if you’re not comfortable with code, it is best not to attempt to manage cookies.
Since you will be editing files on the backend of your website, it is highly recommended to backup your website. This will ensure that if you make a mistake, you can use the backup to revert your website before the mistake was made.
The setcookie() function is used to set cookies in PHP. This will be used in the functions.php file inside of your theme. Inside of the parentheses, you can use the following parameters:
- Name of the Cookie
- Value of the Cookie
- Expiration Date
Only the first two are mandatory. Before you add code, you need to locate your theme’s functions.php file.
Let’s start by logging into the cPanel and clicking on the File Manager option.
You need to locate your theme’s functions.php file. Click on the public_html directory, then click on the wp-content folder. Inside this folder, you will find all of the content related to your website. Click on the themes folder and enter the folder of the theme you are currently using.
Finally, right-click on the functions.php file and select the Edit option.
A pop-up window will show up. This box will warn you to create a backup of your files before editing anything. Click on the «Edit» button.
Upon doing so, a new tab will open containing all of the code from the file.
Copy and paste the following example cookie:
This is a simple example that will create a cookie that time stamps when a user visited your website. You can see it in use if you check the cookies on your browser. It should appear as «time_stamp».
Remember to click on the «Save Changes» button to finish.
You can use this example to set more complicated cookies on your website. You can also remove them at any time by deleting the code.
Setting cookies is very important, but having that timestamp doesn’t quite help if we cannot use the information. So now we need to «get» the information.
In this example, we can call the cookie we just made, time_stamp, and use a shortcode to call the information. In this case, you would see the last time a user visited your website.
You can call your cookies by using the $_COOKIE variable.
Note: Those are not parentheses.
Here is some code that you can place in your functions.php file to accomplish this:
It is a very simple example of what you can do with the cookie you set. It will check if the cookie was set and then if it was, it will display a message with the last time you visited. If it was not set, it will welcome you as a new visitor and set the cookie. You can use the shortcode [greet] to use this information on your website.
Cookies are temporary files and they are not necessary all the time. As such, you should delete them when they are no longer useful.
To do this, you need to use the unset() function.
Deleting the plugin we just made would look like this:
The unset function will delete or unset the cookie and this can be used in many creative ways. Just make sure not to use this out of place because you could essentially set a cookie and then unset it before the use happens, which is pointless.
Cookies can be used to store important information like login credentials. If that gets into the wrong hands, accounts can be compromised. There are multiple regulations that have recently been introduced in the EU.
Thus, if you are doing business there, then you must already be aware and compliant or you will face hefty fines. It is always a good policy to set an expiration date and delete cookies that are no longer necessary.
What kind of cookies do you use? Do you find it hard to manage cookies in WordPress?
Author: Robert Giaquinto
Robert has been writing tutorials about WordPress and other CMS for over 3 years since joining the GreenGeeks marketing team. Thanks to this, he has had the opportunity to research and master several areas of WordPress including plugin usage, SEO, website design, and social media integration. When he is not creating content for WordPress, Robert is digging up new content ideas for environmental pieces. These range from the pollution in our air to the danger’s wildlife face. And with a bachelor’s degree in electrical engineering, he is always eager to discuss the way our technologies are affecting the environment, especially when it comes to solar energy.