Skip to Main Content

Cookies and WordPress: How to create, retrieve and delete

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 901.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

Everyone likes temporary comments, but their virtual use by many websites around the world is often confusing.

The Cookies are a simple solution to keep visitor sessions on the browser, they are also used to keep preferences of visitors. users.

In this tutorial, we'll go through all you need to know about Cookies: what is it, how are they used, and why are we using them (Without eating them).

I would also explain to you why we need to use Cookies on WordPress and end up showing you how to set Cookies manually, how to retrieve them and how to delete Cookies used by your blog.

But before, if you have never installed WordPress discover How to Install a WordPress Blog in 7 Steps et How to search, install and activate a WordPress theme on your blog 

Then back to why we are here.

Everything you need to know about Cookies

Although their use is almost ubiquitous on the web, a large part of users want to know what they are and how they are used. We will first consider the basics you need to master.

What is a Cookie?

A cookie is a small file used by browsers which allows them to save the information of users who interact with websites. They are mostly used for save user sessions.

What is a session?

A session is like an ID card, but I tend to think of it as a pass that allows a user to access a website for a specified period of time. In addition, there are extraordinary sessions (Login information) that confer unique privileges by users.

Why use Cookies to save the session?

There are several reasons that justify the session-cookies combination. Here are some pretty descriptive scenarios.

  • When you add items to the cart (for an eCommerce website), the website saves this information so that your shopping cart keeps its products whether you are on the same page or on another page.
  • If you read information online, the website can back up your IP adress and show you targeted information about your area.
  • Cookies also save information aboutuser authentication for each particular website. This information is used each time the user returns to this website. At this time the cookies will allow them to log in automatically.

Types of Cookies

Like their counterparts in the physical world, Cookies have different flavors:

  • Session Cookies : These cookies save information about user activities in one session. Generally these are permanent information as long as you browse the website, but are erased at the end of your session.
  • Persistent cookies : You will also hear about these Cookies under the designation of Tracking Cookies. These Cookies remain on your computer until they expire or are deleted. They are used to manage user preferences for a long time.

Laws that govern Cookies

Those who do not accept the idea that a website saves their personal information may wonder if this is all legal.

The European Union has spoken on it and it follows the 2009 / 136 / EC directive you will often hear about this law as " European Cookies Act". To cut the story short, legislation requires websites in Europe to ask users for their consent to the saving of a certain type of Cookies.

Outside of Europe the question is how you can use Cookies as the owner of the website, is at your discretion, but the recent mess over the progressive number of users followed by advertisers shows that users are suspicious of and dislike being watched.

Check out articles WordPress GDPR: What it means for your blog for further.

Then tell us what you think

Does my website use Cookies?

If you want to know if your site (Or another) uses cookies on your browser, here is the procedure (On Chrome).

SETTINGS-google

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

Go to the settings and click on " Show advanced options". In the section "Confidentiality", click on " Content settings". A new window will appear showing the options for saving Cookies. Do not touch anything, but click on " Cookies and site data… ”.

The new window that will appear will allow you to search your website or the one of your choice to see the Cookies used.

cookie-google-chrome

From this window, you can also delete Cookies.

Why we need Cookies on WordPress

WordPress does not use Cookies by default. It behaves the same regardless of the visitor. However, it stores Cookies to save user authentication data.

By default, WordPress will not give you the ability to retrieve user data to improve their user experiences. It's important to note that WordPress itself does not keep sessions, but some plugins or themes once activated probably do. 

How to Manipulate Cookies on WordPress

We're going to do a bit of code to handle cookies. Here is what we will do:

  • Send Cookies on HTTP Headers
  • We will write a code in PHP that we will add on our plugin

Remember, I showed you how to create a WordPress plugin quickly. You can add the code in this plugin.

Go further by discovering also How to set, get and delete cookies on WordPress

How to create cookies

To create Cookies in WordPress, you must define values ​​that you want to add to Cookies. In the following example, we will save the name of the user, you will need to add the following portion of code in your plugin:

The constant DAY_IN_SECONDS Is provided by WordPress which contains the number of seconds there are in a day. You will notice that the value is multiplied by 30 or almost a month. The constant "COOKIEPATH" defines the path to your site and "COOKIE_DOMAIN" is the domain name of your site.

After performing the function, you will notice that the Cookie has indeed been added to the browser. Remember to match the values ​​$ v_username and $ v_value.

How to recover Cookies

To retrieve a Cookie that we created instead, we will use the $ _COOKIE variable, which is an associative array that contains all the Cookies created by WordPress.

The username we will use is the one we defined above on the value $ v_username. Look at the following code:


Before passing the identifier to the global variable $ _COOKIE, we must make sure that it does not exist with the function "isset"

Discover 6 WordPress plugins perfect for ensuring GDPR compliance of a blog

How to remove Cookies

Sometimes you will need to delete Cookies from your website, especially when you no longer need information. Look at the following code:


In the previous code, we used the function " a set " to remove the value of the large $ _COOKIE array. To expire the Cookie, we changed the validity date, and we pass an empty string to remove the Cookie value.

Check your website to see if the Cookie has been removed.

If you are used to making backups of your website. The easiest solution would be to restore the version of your blog files.

Discover also some premium WordPress plugins

You can use other WordPress plugins to give a modern look and optimize the grip of your blog or website. 

We offer below some perfect backup WordPress plugins for this task.

1. Backup

Backup is a premium WordPress plugin for backing up and migrating your website. It offers the simplest and fastest tools for backing up, restoring or migrating websites.

Backup

Its features are: unlimited backup, unlimited restore, website migration from one hosting / domain to another, download of backups to computer, planning backups, download and export from Dropbox, Google Drive and Amazon S3, etc.

DownloadDemo | Web hosting

2. Backup And Restore

Backup And Restore is a premium WordPress plugin that will help you schedule backups of your website to a local server or to different cloud servers such as Dropbox, Google Drive and Amazon S3. You will be able to restore your entire website, including the database from existing restore points, with just one click.

Backup And Restore

You will have the possibility to download your website, its database and all its backups. It will also help you to manage backup and restore process in a simple, secure and reliable way.

Its main features include: full backup of the website, backup on Amazon S3, Google Drive, dropbox, the disk space manager, auto-deletion of old backups, complete restoration of your website, multisite support, etc.

DownloadDemo | Web hosting

3. Super Backup & Clone - Migrate

The Super Backup & Clone - Migrate premium WordPress plugin will allow you to back up your website. Indeed, working on your website is always a long and cumbersome process, and it would be a shame if you lost your work due to a bad configuration or a handling error.

Super Backup Clone Migrate for WordPress

It is a trustworthy backup solution that helps you avoid losing your data. Its main functions are:

an intuitive dashboard, the ability to keep multiple instances of backups, the ability to do migration to another server, the possibility of making restore backups, the presence of an automatic diagnosis, the excellent customer support, the management of the multisiteEtc. ...

It is a premium WordPress plugin that we recommend.

Download | Demo | Web hosting

Recommended Resources

Discover other recommended resources that will help you solve the loading performance of your website.

Conclusion

Here is ! That's it for this tutorial. Cookies are one of the things that delay the web, although you will never have to get your hands dirty knowing how to create them, it helps to get a feel for how they work. We've gone through the basics you need to know about Cookies and how to handle them on your blog.

However, you will also be able to consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on the WordPress blog creation.

But, in the meantime, tell us about your Comments and suggestions in the dedicated section and above all share this tutorial on your favorite social networks.

... 

This article features 1 comment

  1. Hello,
    I developed a web application on the same server as a wordPress site. When I run into a 404, all the cookies (as well as the localStorage) are cleaned, and the user of my application is logged out. Is there a solution to prevent this ??? Thank you

Leave a comment

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

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top
2 shares
Share1
tweet1
Save