Explaining cookies and their use in the WordPress system

Have you ever wandered into a bakery looking for donuts and found their name “cookies”? This literal translation taken from Google for donuts goes back to a technical term that was introduced in the year 1994 when e-commerce developed, and the need required the presence of a memory for websites to remember their visitors and their preferences in order to show them content that attracts them, matches their tastes, and wins them over as real customers, and the development of advertising and publicity tools.

The reason for the name goes back to (Fortune cookie), which is an ancient Chinese tradition, which is a piece of sweetened cake that carries a message inside it for the eater, often a motivational and positive message. Likewise, cookies are small files that carry a message or information inside them.

Definition of cookies

Cookies are small files that are created when you visit various websites on their servers. They store your login information such as name and email, and browsing information such as preferences, pages viewed, items added to the shopping cart, etc.

These files are assigned a unique identifier for your computer, so that it can identify you on your next visits. The site’s server receives the identifier when you access it again and requests the information it has to improve the user experience and provide content more appropriate to what the visitor prefers to browse, and thus it identifies the computer as a single user. Re-visit a site. 

Therefore, not saving this information means logging in anonymously every time to a site, and losing content that you like. It means re-creating your favorites list or shopping cart if you are in an online store, for example, and you will see fashion news instead of economic news as well.

Not saving them also means logging in and writing your email and name every time you visit your website, which you frequently visit, and other disadvantages of not saving cookies. Its name refers to the link between the user’s device and the website, so that it is a tool for identifying the latter’s website, which was mentioned previously. 

But cookies may constitute a loophole through which your privacy is violated by lurkers and spies! Therefore, you must learn how to delete and remove them whenever you doubt something about a site, or feel uncomfortable about how a particular site uses these files, but it has its benefits that we touched upon a moment ago. 

There are two types of cookies: 

  • Created directly by the site you are visiting.
  • Created by a third party, such as the owners of images or advertisements on the sites you visit.

Use of cookies in WordPress

The use of cookies by WordPress and the sites built on it is no different from the use of other sites, but it mainly uses cookies in two ways: 

  • User session cookies, which store login authentication data and are exclusive to the admin screen. The data is stored in an encrypted form and kept for a period of 15 days. 
  • Comment cookies: They store the information that the comment writer enters on different WordPress sites, such as name and email, so that he does not have to retype them every time he wants to comment on a different site. 

It is also worth noting that third-party cookies, such as WordPress plugins, are also among the types of files used in WordPress, but they are not essential because not all WordPress plugins use cookies, each according to its function and mission. Some of them may stop working without files. Cookies are like analytics plugins that need some information to analyze and clarify.

Now we are going to learn how to add cookies to your site on WordPress, and it is very simple. First, you have to go to the ( Appearance ) tab in the side menu of the site’s control panel, then choose ( Template Editor ) from the submenu, and access the functions.php file to edit it.

When you click on the template editor, you will find a side window on the left of the screen that displays the template files, and as shown in the following image, we find within it the functions.php file (template properties).

 Click on it to modify it and write a code that activates cookies on your WordPress site. The code is a function previously defined in PHP to set cookies. It receives several parameters, one of which is the name of the cookie, its value, its validity period, that is, how long it remains saved for you, and other data. You can choose any place within functions.php to write the code. 

They are as follows: 

function cookies_timestamp() {
$visit_time = date(‘F j, Y g:i a’);
if(!isset($_COOKIE[$visit_time])) {
setcookie(‘visit_time’, $current_time, time()+86400);
}
}

This image shows adding the code. Click on ( Update File ) to save your modifications, then let us verify its activation. 

You can verify that your cookies have been added by clicking the right mouse button anywhere on your site’s screen where this list will appear, then clicking on the last option ( inspect ).

Then this screen will appear to us, and from there we will click on the ( application ) tab appearing at the top of the screen as shown.

After clicking ( apply ), this screen will appear to us and show that your cookies are activated, with their details such as scope, path, size, etc., and thus things will go as they should. 

After ensuring that your cookies are activated, it is time to retrieve and use them. Bringing them means displaying them, viewing them, and entering them into the processing processes to benefit from them. This code can be used to perform this process: 

function get_cookie() {
$visit_time = date(‘F j, Y g:i a’);
if(isset($_COOKIE[‘visit_time’])) {
function placeholder() {
}
}
}

In the first code, we set an expiration date for cookies, which means they will be deleted after a certain time. However, some situations must occur that force you to delete cookies manually before the expiration period expires, so you can use this code:

unset($_COOKIE[‘visit_time’]);

By copying and pasting it into your site’s functions.php file.

Here we have completed our discussion about cookies, and discussed their definition and how to use and benefit from them. We hope that the article will be useful and useful to you. 

Avatar photo
I am a young man who has been working in WordPress and e-marketing for 10 years. I would like to share my experience with you so that we can become professional in WordPress I will be happy to share the experience with you.