How to Manage Cookies in WordPress

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.

Many plugins also use cookies to store information that is needed to work properly. Cookies are used on almost every single website and are an essential part of running a website. Unfortunately, they are very hard to understand and will require some prior coding knowledge. Today, I will demonstrate how to manage cookies in WordPress.

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.

Click on the Settings option.

Type “Content Settings” into the text box at the top of the screen. Click on the Content Settings option.

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.

Click on 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.

Set Cookies

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
  • Path
  • Domain
  • Secure
  • HttpOnly

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.

Click 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.

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.

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:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function set_cookies() {

$visit_time = date(‘F j, Y g:i a’);

if(!isset($_COOKIE[$time_stamp])) {

// set a time stamp cookie that will last 1 year
setcookie(‘time_stamp’, $current_time, time()+31556926);



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.

Click on the "Save Changes" button.

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.

Get Cookies

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:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function get_cookies() {
// The user’s visit time
$visit_time = date(‘F j, Y g:i a’);

// Check if the cookie was previously set
if(isset($_COOKIE[‘time_stamp’])) {

// If the cookie is set, continue with this
function visitor_message() {

// Use the Cookie
$lastvisit = $_COOKIE[‘time_stamp’];

$string .= ‘Thanks for visiting ‘. $lastvisit .’. Check out whats new’;

return $string;

} else {

// If the cookie was not set, do this
function visitor_message() {
$string .= ‘We see that your new and want to welcome you! Check out these resources…’ ;
return $string;

// Set the cookie
setcookie(‘time_stamp’, $visit_time, time()+31556926);

// Add a shortcode
add_shortcode(‘greet’, ‘visitor_message’);

add_action(‘init’, ‘get_cookies’);[/ht_message]

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.

Delete Cookies

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:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]unset($_COOKIE[‘time_stamp’]);[/ht_message]

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.

Use Cookies Responsibly

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.

This is not just for safety either, cookies will slow down web browsers, which is why you should clean them every few months. Always consider how you can use cookies to your website’s advantage.

What kind of cookies do you use? Do you find it hard to manage cookies in WordPress?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.