Would you like to learn how to use cookies on your WordPress website?

Cookies are useful tools to store temporary information on the user's browser, finally to be able to use this information to improve user experience through customization and behavioral targeting.

In this tutorial, we will show you how to set, get and delete cookies on WordPress.How to set get delete wordpress cookies 1

Note: This is an advanced tutorial. This requires that you have perfect command of HTML, CSS, WordPress and PHP.

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

What is a Cookie?

Cookies are text files that are created and stored in users' browsers when they visit a website. Cookies are used to add different functionality to a website.

Here is some of the common use of cookies on various websites.

  • Store and manage user login information
  • Store temporary session information when a user visits
  • Online stores use cookies to remember items in the shopping cart when a user visits
  • Tracking user activity on a website to provide a personalized user experience
  • and more

As you can see, cookies are very useful tools for website owners, but they can also be a bit intrusive. Recent trends in email marketing, growth hacking, and online marketing as a whole allow websites to set cookies that serve as a beacon and can be used to store and even share user activity across websites. Web sites.

This is the reason why the European Union passed the European Cookie Law, which requires website owners to declare that they use cookies to store information.

So much to see... 6 premium WordPress plugins to ensure GDPR compliance of a blog

How cookies are used in a WordPress website

By default, WordPress uses cookies to manage user sessions and authentication. It also uses cookies to remember a user's name and email address if they fill out a online form of comment.

However, many WordPress plugins on your website may also set their own cookies. If you use third-party services on your website, such as Google Analytics ou Google AdSensethey can also create cookies on your website.

You can see all the cookies of the website in the settings of your browser. For example, in Google Chrome you need to access settings and search Content settings.

access Google cookie settings chrome.jpeg

In the content settings, you must click on " Cookies To open the cookie settings page.

cookie list chrome.jpeg

Then you must click on the option "All cookies and website data".

show all site.jpeg cookies

On the next page you will see a list of all the cookies and data stored on your browser by all the websites you visited.

Discover How to use the non-persistent cache of WordPress

You can type a website address in the search field, and it will show you the data stored by this website.

list of cookies.jpeg

By clicking on a single article, you will see more details about individual cookies and their content.

How to set a cookie on WordPress

To follow this tutorial, you must add code to functions.php file of your theme or on a WordPress plugin. If you haven't already, take a look at our guide on how to copy and paste snippets in WordPress.

We will first use the function setcookie() on PHP. This function accepts the following parameters.

  • Cookie name
  • Cookie value
  • Expires (Optional: Defines a period after which the cookie expires)
  • Path (Optional, by default, it will use the root of the website)
  • Domain (optional, defaults to the domain of your website)
  • Secure (Optional, if true, only transfers cookie data via HTTPS)
  • httponly (Optional, if set to true the cookie is only accessible via HTTP and cannot be used by scripts)

Now let's add a code snippet to your WordPress website. This code stores in a cookie the exact timestamp when a user has visited your website.

function bpc_cookies_tutorial1 () {$ visit_time = date ('F j, Y g: i a'); if (! isset ($ _ COOKIE [$ bpc_visit_time])) // set a cookie for 1 year setcookie ('bpc_visit_time', $ current_time, time () + 31556926); }}

You can now visit your website and check your browser's cookies. You will find a cookie with the name bpc_visit_time.

How to get a cookie and use it on WordPress

Now that we have created this cookie stored in the user's browser for a year, let's take a look at how to use this information on our website.

If you know the name of a cookie, you can easily call it anywhere on PHP using the variable $ _COOKIE []. Let's add some code that not only sets the cookie but also uses it to do something on your website.

function bpc_cookies_tutorial2 () {// Set the visit time $ visit_time = date ('F j, Y g: i a'); // Check that the cookie exists if (isset ($ _ COOKIE ['bpc_visit_time'])) {// Do this if the cookie exists function visitor_greeting () {// Used the information saved on the cookie $ lastvisit = $ _COOKIE [' bpc_visit_time ']; $ string. = 'Your last visit to our site'. $ lastvisit. '. Discover what's new '; return $ string; }} else {// Do this if the cookie does not exist function visitor_greeting () {$ string. = 'Are you new? Discover these resources ... '; return $ string; } // Set the cookie setcookie ('bpc_visit_time', $ visit_time, time () + 31556926); } // Add the shortcode add_shortcode ('greet_me', 'visitor_greeting'); } add_action ('init', 'bpc_cookies_tutorial2');

We've commented on the code to show you what each part does. This code uses the information stored in the cookie and displays it with the help of a shortcode. You can now add a shortcode [Greet_me] anywhere on your website, and it will then display a user's last visit.

Feel free to modify the code to make it more useful for your website. For example, you can view recent posts for returning users and popular posts for new users.

Deleting a cookie on WordPress

So far we have learned how to set a cookie and use it later on your website. Now let's see how to delete a cookie.

To delete a cookie, you must add the following line to your code.

unset ($ _ COOKIE [ 'bpc_visit_time']);

Remember to replace bpc_visit_time with the name of the cookie you are trying to delete.

Let's put this code in context using the same sample code we used above. This time, we will delete a cookie and set it again with new information.

function bpc_cookies_tutorial2 () {// Time of user 's visit $ visit_time = date (' F j, Y g: i a '); // Check if cookie is already set if (isset ($ _ COOKIE ['bpc_visit_time'])) {// Do this if cookie is set function visitor_greeting () {// Use visit information $ lastvisit = $ _COOKIE ['bpc_visit_time ']; $ string. = 'Your last visit'. $ lastvisit. '. discover what's new '; // remove a cookie to refresh it unset ($ _ COOKIE ['bpc_visit_time']); return $ string; }} else {// Fiare this if a cookie does not exist function visitor_greeting () {$ string. = 'are you new? Check out these resources ... '; return $ string; }} add_shortcode ('greet_me', 'visitor_greeting'); // Set or reset a cookie setcookie ('bpc_visit_time', $ visit_time, time () + 31556926); } add_action ('init', 'bpc_cookies_tutorial2');

As you can see, this code deletes the cookie once we have used the information stored inside. We later updated the cookie with the time information.

Recommended Resources

We also invite you to consult the resources below to go further in the grip and control of your website and blog.

Conclusion

Here ! That's it for this tutorial. We hope this article has helped you learn how to easily configure, get, and delete WordPress cookies. If you liked this article, ndo not hesitate to share on your social networks preferred.

However, do not hesitate 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 or the one on Divi: the best WordPress theme of all time.

If you have some Comments or any suggestions on how to monetize your blog through affiliate marketing? Let us know in the section below.

 ...