How to Set and Get WordPress Cookies

How to Set and Get WordPress Cookies

WordPress cookies are useful tools that enable you to store temporary information in the browser of a visitor. You can then use this information to improve user experience through behavior targeting and personalization. In this post, we shall be describing the steps you need to take to set, get and delete cookies on WordPress.

What are cookies?
Cookies are simply plain text files which are created and stored on the browser of a website visitor. Cookies add various features to a website. Here are the main usages of WordPress cookies:

  • Store and manage login information of users
  • Track user activity on a website so as to offer a more personalized user experience
  • Storage of temporary session information during a visit
  • Ecommerce stores use the cookies to remember cart items after a user visit

Cookies can be used in many more ways. They are particularly useful to the website owners. It is important to note that they can also be quite invasive. They have been linked to recent trends in growth hacking, email marketing and online marketing. Cookies are used as beacons and can even be used to share user activity online. If you must use cookies, you need to let your visitors know that your website uses cookies.

How to set a WordPress cookie
To setup a cookie, you need to head to setcookie () function in the .php file. This function will accept these parameters:

  • Cookie name
  • Cookie value
  • Cookie expiry period
  • Path
  • Domain
  • Secure
  • Httponly

With the understanding of the above information, you can go ahead and add a cookie to your website. An example code goes as follows:
Function wpb_cookies_tutorial1() {
$visit_time = date (‘F j, Y g:I a’);
if (!isset ($_COOKIE [$wpb_visit_time])) ] {
// set a cookie for 1 year
setcookie(‘wpb_visit_time’, $current_time,
time()+31556926
}
}

That’s all that is needed to set a cookie. When using this code, you will find the WordPress cookie with the name wpb_visit_time.

How to get and use a cookie
If you know the name of a cookie, you can call it anywhere in the PHP using the $_COOKIE[] variable. Here is a simple code that does something.

Function wpb_cookies_tutorial12() {
// Time of user’s visit
$visit_time = date (‘F j, Y g:I a’);

// Check if cookie is already set
if (!isset ($_COOKIE [$wpb_visit_time])) ] {

//Do this if cookie is set
Function visitor_greeting () {

//Use information stored in the cookie
$lastvisit = $_COOKIE [‘wpb_visit_time’]

$string .=’You last visited our wesite ‘. $lastvisit
.’. Check out whats new’;

Return $string;
}
}else {
//Do this if the cookies doesn’t exist
Function visitor_greeting () {
$string .= ‘New here? Check out these resources…’ ;
Return $string;
}

// set a cookie
setcookie(‘wpb_visit_time’, $current_time,
time()+31556926
}

//Add a shortcode
Add_shortcode (greet_me’, ‘visitor_greeting’);
}
Add_action (‘init’, ‘wppb_cookies_tutorial12’);

The above code shows you how each line of the code does. The code relies on the data stored in the cookie and outputs it using shortcode. You can modify the code the way you want.

WordPress cookies