How to Set, Get, and Delete Cookies on WordPress

If you are looking for full package information about how to set, get, and delete cookies in WordPress, this article is for you. Different from occasional cookies that stimulate your sweet cravings, virtual cookies often bring some confusion for Internet users.

In this article, we will explain to you what WordPress cookies are, why every website is using them, as well as how to delete them from your site. We’ll also provide a handy tip for protecting your WordPress website and control the cookies even better.

Be prepared because in order to set, get, and delete cookies in WordPress, you have to do it manually and understand coding a little bit.

Now let’s get started!

What Are Cookies?

Cookies are small text files created and sent to users’ browsers whenever they visit a website. It saves all the information about users including their usernames, passwords, add-to-cart history on an e-commerce store, and so on.

Anywhen a user visits your website, thanks to the cookies stored in the user’s browser, you can track his previous activities. For several purposes, cookies play an important role in any website, especially in helping understand users’ experience better.

What Cookies Are Used For?

On various websites, cookies usages commonly include:

  • Store temporary sessions information while users visit a website
  • Save and manage user login details
  • Remember cart items when users shop on a website
  • Store IP information to show relevant news based on users geographical location
  • Personalize user experience by tracking user activity on the website

It’s very useful for website owners to understand their customers, and adjust their website for better users’ experience. Business owners take advantage of cookies to build better marketing strategies. For example, they can analyze the location of visitors coming to their site then target those markets to boost sales.

However, since cookies can track every activity of users, it will lead to some privacy problems. Even though websites using cookies always show a cookie policy pop-up to inform customers, some of them often ignore and underestimate it. With growth hacking, online marketing, cookies can be shared across websites leading to personal information leak issues.

WordPress Cookies

WordPress, in fact, doesn’t store cookies to remember shopping carts or visitors’ data to improve users’ experience. Instead, it saves cookies for user authentication, especially when you log in to your website’s backend.

However, even though WordPress chooses other ways to enhance user interaction, many themes and plugins in your WordPress site still use cookies. Hence, the WordPress site’s cookies saved in your browser are mainly from other plugins and themes.

Set Cookies in WordPress

To set cookies in WordPress, you need to add code to the theme’s function.php file. Using the setcookie() function which includes the following syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

Now simply set the value that you want to store. For example, to store the username of visitors, you might need to type the code like this

 

<?php

    add_action( 'init', 'my_setcookie' );

    function my_setcookie() {

   setcookie( $visitor_username, $username_value, 15 * DAYS_IN_SECONDS, COOKIE_PATH, COOKIE_DOMAIN );

    }

?>

 

In this code, the DAYS_IN_SECONDS is provided by WordPress as a default. 15 * DAYS_IN_SECONDS indicates that your cookies will expire after 15 days. The two variables COOKIE_PATH, and COOKIE_DOMAIN define your site’s path and domain and are set by your site.

Get WordPress Cookies

In case you want to retrieve the cookie created before, use the $_COOKIE variable. It gets the information of all the cookies that you’ve added to your browser. However, to get the cookie value, you should locate it in the array by name.

 

<?php

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

    echo "The cookie: '" . $visitor_username . "' is not set.";

    } else {

    echo "The cookie '" . $visitor_username . "' is set.";

    echo "Cookie: " . $_COOKIE[$visitor_username];

    }

?>


Make sure the cookie was set before you pass the cookie’s name into the $_COOKIE variable. The isset() function will return to TRUE if the cookie has been set and FALSE if it wasn’t.

Delete Cookies in WordPress

After setting and getting cookies, you might wonder what if you want to delete them. Fortunately, cookie manipulation in WordPress is pretty simple.

You will use the unset() function to remove or delete the unwanted cookie from the $_COOKIE array. So after unsetting the cookie, you need to use the same function to delete it. Confused, right? Just look at the code below

 

<?php

    unset( $_COOKIE[$visitor_username] );

    setcookie( $visitor_username, '', time() - ( 15 * 60 ) );

?>

 

Once unsetting the cookie to, use the setcookie( $visitor_username, ”, time() – ( 15 * 60 ) to set the cookie’s expiration to an empty value.

Notice that if you delete the cookie, you’d better redirect users to another page. Ideally, it should be your WordPress homepage. Using this code to do that:

 

wp_redirect( home_url(), 302 );

exit;

Using Password Protect WordPress to Protect and Set Cookies for your Content

As you know, WordPress doesn’t use cookies to track users’ activity but its plugins do. Password Protect WordPress turns out to be one of them.

Besides being well-known as the top plugin to help WordPress users to lock their private content with passwords, the plugin also lets you control the cookie for each password-protected content.

Install and set up the plugin

The 3 simple steps below will help you install and use the plugin easily.

  • 1: Download Password Protect WordPress Pro
  • 2: In your WordPress admin dashboard, find PluginsAdd NewUpload Plugin and open the downloaded zip file
  • 3: Install the plugin and check your mailbox to get the license key to activate the plugin
  • 4: Head to your desired posts or pages, and click “password protect.”

 

The plugin will automatically generate a strong password for each protected content. You are allowed to change the password or create multiple passwords for one certain page or post. Inside the password pop-up window, you have full control over the password usage, quick access links, and password deactivation.

Using Password Protect WordPress Pro to set cookies for protected content

Look for the Password Protect WordPress setting in your dashboard to see what you can do with the cookies.

 

In the General tab, there is a section to set Cookie Expiration Time. This cookie helps your browsers remember the password that users have entered to the protected content. So, the next time they access that content, they don’t have to re-enter the password.

The expiration time values could be Days, Hours, or Minutes, you just need to set the number for it.

Password Protect WordPress Pro allows you to manage your password cookies effectively. On one side, it saves users time so that they don’t have to enter passwords when accessing protected content again. On the other side, expiring password cookies protect your protected content against unauthorized users and maintains its security.

Cookie’s Always a Lovely Sweet if You Have a Right Recipe

Despite the fact that the only way you can manage your WordPress cookies is by using code, it is still not too complicated to follow with our detailed instructions. You can gain a lot when making use of cookies, especially in improving customers’ experience.

However, there are still downsides of cookie usage. Some organizations can use the cookie information stored on your website for bad purposes. Hence, most reputable websites have a privacy policy and terms of service pages to explain to users how they use cookies on their websites.

As always, protection should be the first priority. With Password Protect WordPress Pro, you get the full package of managing password protection and password cookies for specific pages, posts, and custom post types on your websites. The installation and protection are easy to follow facilitating you to protect your valuable content effortlessly.

Let us know if you have any questions relating to cookies and content protection in the comment section below.

Leave a Reply

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