In this tutorial: cookies and WordPress, I will show you how to manipulate – create, retrieve, delete – Cookies on your WordPress blog

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 e-Commerce 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

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 - cookies and WordPress

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. It does, however, store 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:

<?php

add_action( 'init', 'setting_my_first_cookie' );

function setting_my_first_cookie() {
 setcookie( $v_username, $v_value, 30 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
}

?>

The constant DAY_IN_SECONDS is provided by WordPress which contains the number of seconds 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 identifier we will use is the one we defined above on the $v_username value. Look at the following code:

<?php

if(!isset($_COOKIE[$v_username])) {
 echo "The cookie: '" . $v_username . "' is not set.";
} else {
 echo "The cookie '" . $v_username . "' is set.";
 echo "Cookie is: " . $_COOKIE[$v_username];
}

?>

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:

<?php

 unset( $_COOKIE[$v_username] );
 setcookie( $v_username, '', time() - ( 15 * 60 ) );

?>

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 appearance and to optimize the handling of your blog or website. 

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

1. Easy Social Share Buttons

Easy Social Share Buttons offers one of the longest feature lists I have ever seen. Depending on your specific needs, it could be a good or a bad thing. But no one can say that you do not have enough options with this plugin.

Easy Social Share Buttons for WordPress

Among its features you will find among others:

the ability to customize your tweets, the actions after sharing, the ability to view detailed analytics of your button performance and even run A / B tests to try to strengthen your actions, recovering the number of lost shares if you change domain or if you go to HTTPS, and many others.

Discover more How to view articles from a category on WordPress

The fact that Easy Social Share Buttons has maintained a rating of 4,66 stars (on 5) on more than 24 000 sales suggests that many people appreciate its degree of functionality.

Download | Demo | Web hosting

2. Layer Slider

Layer Slider is by far the most popular of the slider plugins. It is the most professional slider with the famous parallax effect and hundreds of transitions. You can use LayerSlider to create an image slider or a rotating banner for your blog.

LayerSlider Responsive WordPress Slider Plugin

It has tons of customizable features and options. You'll have the ability to create slides with the amazing WYSIWYG slide editor, and you'll even be able to create new Slider transitions with ease.

Read also our article on 8 WordPress plugins to create a megamenu on your blog

So if you are looking for a WordPress Plugin priced slider that does it all, Layer Slider is your best bet, even if its documentation isn't great. Also, it takes an average of 24 hours for the plugin author to respond. (time difference required). But in terms of features you will be entitled to everything that this plugin claims to offer.

Download | Demo | Web hosting

3. Super Backup & Clone - Migrate

Le WordPress Plugin premium Super Backup & Clone – Migrate will allow you to backup 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 following a bad configuration or a manipulation 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’s a WordPress Plugin premium 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.

...