How to Set Cookies in WordPress

Setting Cookies in WordPress. As a budding WordPress developer or hobbyist, once you've become familiar with the basics of WordPress, you'll no doubt want to experiment further by developing your own widgets and plugins, or by creating your own themes, etc. And, along the way you'll at some point want to know how to set cookies in WordPress. Once you know how to do it, you'll realize how simple it is to accomplish.

Although cookies often get a bad rap, they can be immensely useful in all kinds of ways, and have many more uses than a common perception that they're simply used/abused for nefarious tracking purposes. Here we explain how to set cookies in WordPress, and how to retrieve their values, along with how to delete WordPress cookies.

How to Set WordPress Cookies

Setting a Cookie: The example below will set a cookie which expires in one week. The cookie is set with COOKIEPATH and COOKIE_DOMAIN which are defined by WordPress according to your site path and domain. You don't need to create these variables or do anything with them; they already exist. The example also uses the WordPress time constant WEEK_IN_SECONDS, which is explained below the example.

So, in your PHP script, at the point where you wish to set your cookie (or you may want to put the code into a function which can be called from elsewhere) simply use this code:

setcookie( 'cookie-name', 'cookie-value', time() + WEEK_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

You could equally have used a numeric value such as 604800 (the number of seconds in a week) instead of using the constant WEEK_IN_SECONDS, but using the WordPress time constants are eminently more readable and easy to quickly understand. Other WordPress time constants include:


It should be clear that 'cookie-name' and 'cookie-value' are whatever you decide; for example, if you're setting a user-defined background color, you might have a cookie name of 'color' and a cookie value of 'red'.

Retrieve WordPress Cookie Values

Retrieving a Cookie: Retrieving the value of a WordPress cookie is equally simple, as demonstrated below, using the WordPress $_COOKIE variable, which contains an associative array.

$cookie_value = isset( $_COOKIE['cookie-name'] ) ? $_COOKIE['cookie-name'] : 'xxx';

This code uses the ternary operator to either provide the cookie value, if it exists, or to set the value of $cookie_value to 'xxx' (or to whatever you like) if it does not exist. So, if we were to extend the idea of setting the user-defined color, and you want to retrieve the cookie named 'color', then you might do this:

 $bgcolor = isset( $_COOKIE['color'] ) ? $_COOKIE['color'] : 'red';

Hopefully this is easily understandable; you attempt to retrieve the value of the cookie named 'color', and set the value of $bgcolor to the cookie value. If the cookie does not exist then $bgcolor defaults to 'red'.

How to Delete WordPress Cookies

Deleting a WordPress cookie: You can easily delete (i.e. expire) a WordPress cookie using the same method as we demonstrated to set a cookie. It's the exact same code, except for the fact that you provide a negative time value: note in the example below we have time() - WEEK_IN_SECONDS instead of time() + WEEK_IN_SECONDS:

setcookie( 'cookie-name', '', time() - WEEK_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

Two things mentioned in the article, which you may want to know more about, were "ternary operators" and the WordPress "time constants": more information about those can be found at the links below:

Hopefully some of the information above will prove useful to you in learning how to set, retrieve, and delete/expire WordPress cookies.