Adding Custom Cookie To WordPress

Solution:1

Bumped into this one – I recommend against adding a new cookie, instead I would hijack (take advantage of) the current cookie and let WP manage it for you. Additionally the hooks available in WP allow very clean and tight code using WP features – try the snippet below – I put in comments and tried to be verbose :

function custom_set_newuser_cookie() {
    // re: http://codex.wordpress.org/Function_Reference/get_currentuserinfo
    if(!isset($_COOKIE)){ // cookie should be set, make sure
        return false; 
    }
    global $current_user; // gain scope
    get_currentuserinfo(); // get info on the user
    if (!$current_user->user_login){ // validate
        return false;
    }
    setcookie('sitename_newvisitor', $current_user->user_login, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); // change as needed
}
// http://codex.wordpress.org/Plugin_API/Action_Reference/wp_login
add_action('wp_login', 'custom_set_newuser_cookie'); // will trigger on login w/creation of auth cookie
/**
To print this out
if (isset($_COOKIE['sitename_newvisitor'])) echo 'Hello '.$_COOKIE['sitename_newvisitor'].', how are you?';
*/

And yes, use functions.php for this code. Good luck.

Solution:2

How to Set Cookies in WordPress (2 Steps)

You’ll need to use PHP to create and set up cookies in WordPress. Where you add the necessary code depends on whether you want to use your theme or a custom plugin. Let’s take a look at how the first method works.

Step 1: Open Your Theme’s functions.php File

In most cases, the theme approach is the easiest route to take. To set a new cookie, you’ll want to edit your active theme’s functions.php file.

First, access your website via FTP and navigate to the public_html/wp-content/themes directory. Inside, you’ll find individual folders for each theme that’s installed on your website.

Open your active theme’s folder, and look for the functions.php file inside. To add a custom cookie, you’ll need to include some additional code within this file. Before that, however, you need to understand what parameters you can use:

  • The name of the cookie
  • Its value
  • How long until it expires (it can’t last forever!)
  • Which pages the cookie will act on
  • Your domain and/or subdomains
  • Whether it should transfer over HTTP or HTTPS

We’re going to use most of these parameters within the next section, so don’t worry if you don’t fully understand what each of them does just yet.

Step 2: Add Your New Cookie’s Code

Once you open the functions.php file, you’ll be able to add custom code to it. Here’s an example of the code you’d use to add a new cookie:

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

That code includes three of the parameters we laid out in the last section. There’s the cookie name (cookies_timestamp), its value (visit_time), and how long until it expires.

What this particular cookie does is generate a timestamp of the last time someone visited your site. You might then use the cookie to display a message such as: “Your last visit was on January 25th, 2019”. This lets users know if someone else has accessed their account.

As for the expiration time, you’ll notice that it uses seconds. We set the value for a day, which is pretty short by cookie standards. The rest of the parameters don’t matter as much, because the default options work well enough in almost every case.

When you’re done configuring your cookie, save the changes to functions.php and close it. Then, your cookie will start working right away!