How to Set, Get, and Delete Cookies on WordPress

If you’re looking for comprehensive information on how to set, retrieve and delete cookies in WordPress, this article is for you. Unlike occasional cookies that whet the sweet tooth, virtual cookies often cause confusion for internet users.

In this article, we explain what WordPress cookies are, why every website uses them and how you can delete them from your website. We also give you a practical tip on how you can protect your WordPress website and control cookies even better.

Be prepared, because to set, retrieve and delete cookies in WordPress, you need to do it manually and understand a little bit of programming.

What are cookies?

Cookies are small text files that are created and sent to users’ browsers when they visit a website. They store all information about users, including their usernames, passwords, order history in an e-commerce store, and so on.

When a user visits your website, you can track their previous activities thanks to the cookies stored in the user’s browser. For various purposes, cookies play an important role on any website, especially to better understand the user’s experience.

So what are cookies used for?

Cookies have a variety of different uses for your website. Let’s look at some of the most important:

  • Storing temporary session information during a visit to a website
  • Saving and managing user login data
  • Storing items in the shopping cart when users shop on a website
  • Store IP information to display relevant messages based on the user’s geographic location
  • Personalize the user experience by tracking user activity on the website

For website owners, understanding your customers means you can then customize it to make it more usable for users – most companies take advantage of cookies to develop better marketing strategies. For example, they can analyze the location of visitors to their website and then target those markets to increase sales.

However, since cookies can track every user activity, this can lead to privacy issues. Although websites that use cookies always display a cookie policy pop-up to inform customers, it is often ignored or underestimated. And with cookies being a crucial element of online marketing, cookies being used on your website can lead to problems with personal data.

WordPress cookies

WordPress does not store cookies to remember shopping carts or visitor data to improve the user experience. Instead, it stores cookies for user authentication, especially when you log in to the backend of your website.

Even though WordPress chooses other ways to improve user interaction, many themes and plugins on your WordPress website still use cookies. The WordPress website cookies that are stored in your browser, therefore, mainly come from other plugins and themes.

How to set cookies in WordPress

To set cookies in WordPress, you need to add code to the function.php file of the theme. Use the setcookie() function, which contains the following syntax:

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

Now simply enter the value you want to save. For example, to save the user name of the visitors, you must enter the code as follows:

<?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 value DAYS_IN_SECONDS is set by WordPress as the default. The 15 * DAYS_IN_SECONDS indicates that your cookies will expire after 15 days. The two variables COOKIE_PATH and COOKIE_DOMAIN define the path and domain of your website and are set by your website.

Retrieve WordPress cookies

If you want to retrieve the previously created cookies, use the $_COOKIE variable. It provides information about all cookies that you have saved in your browser. However, to retrieve the value of the cookie, you must search for it by name in the array.

<?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 that the cookie has been set before you enter the name of the cookie in the $_COOKIE variable. The isset() function returns TRUE if the cookie has been set and FALSE if it has not been set.

Delete cookies in WordPress

After you have set and retrieved cookies, you may be wondering what to do if you want to delete them. Fortunately, manipulating cookies in WordPress is pretty simple.

You use the unset() function to remove or delete the unwanted cookie from the $_COOKIE array. After you reset the cookie, you need to use the same function to delete it. Confused, right? Just take a look at the following code:

<?php
unset( $_COOKIE[$visitor_username] );
setcookie( $visitor_username, '', time() - ( 15 * 60 ) );
?>

Once the cookie has been deleted, use setcookie( $visitor_username, ”, time() – ( 15 * 60 ) to set the cookie expiration date to an empty value.

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

wp_redirect( home_url(), 302 ); exit;

Password protection in WordPress to protect and set cookies via plugin

As you know, WordPress does not use cookies to track user activity, but its plugins do, and Password Protect WordPress Pro is one of them. The plugin allows you to protect their private content with passwords, but also lets you control the cookies for any password-protected content.

With the following 3 simple steps, you can easily install and use the plugin.

1: Download Password Protect WordPress Pro
2: In your WordPress admin dashboard, search for Plugins → Add New → Upload 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: Go to the desired posts or pages and click on “Password protection”

The plugin will automatically generate a strong password to protect your content. You have the option to change the password, or you can create multiple passwords for a specific page or post.

Within the password pop-up window, you have full control over your password usage, quick access links, and password deactivation.

Using Password Protect WordPress Pro to set cookies for protected content

Go to the Password Protect WordPress setting in your dashboard.

On the General tab, you will find a section where you can set the expiration time of cookies. This cookie helps your browsers to remember the password that users have entered for the protected content. So, the next time they access this content, they will not have to enter the password again.

The expiration time can be specified in days, hours or minutes – you just need to set the appropriate number.

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

Conclusion

Although managing your WordPress cookies requires some coding ability, it’s still relatively straightforward. And if you want to improve the customer experience, it’s worthwhile investing in.

However, cookie usage comes with important privacy considerations.. Some organizations can use the cookie information stored on your website for bunauthorized purposes. Therefore, most reputable websites have a privacy policy and terms of use page to explain to users how they use cookies on their websites.

Security should always be your priority. With Password Protect WordPress Pro, you get the complete package for managing password protection and password cookies for specific pages, posts and custom post types on your websites.