Avatar of Jon Imms
Jon Imms
Flag for United States of America asked on

Set a time limit on Wordpress Cookie function.

I have the following function on our site,  which captures UTM tracking codes in cookies.  Users click on external ads, or through external sites to get to our site, and the UTM is stored in the cookie.

function set_ad_cookies() {
  $vars = array('utm_source', 'utm_campaign', 'utm_media');
  //$parsed = array();
  foreach ($vars as $k) {
      if (isset($_GET[$k]) && !isset($_COOKIE['orig_'.$k])) {
          setcookie('orig_'.$k, $_GET[$k], 0, '/', $domains['cookie_domain']);
      }
	  
      /*if (isset($_COOKIE['orig_'.$k])) {
          $parsed[$k] = $k.'='.urlencode($_COOKIE['orig_'.$k]);
      } elseif (isset($_GET[$k])) {
              // no need to handle get params here - they will be passed with $_SERVER['REQUEST_URI']
      #    $parsed[$k] = $k.'='.urlencode($_GET[$k]);
      }*/
  }
  
    if (!isset($_COOKIE['orig_utmsource'])) {
	  setcookie('orig_utmsource', $_GET['utm_source'], 0, '/', $domains['cookie_domain']);
	}
	if (!isset($_COOKIE['orig_utmmedium'])) {
	  setcookie('orig_utmmedium', $_GET['utm_medium'], 0, '/', $domains['cookie_domain']);
	}
	if (!isset($_COOKIE['orig_utmcampaign'])) {
	  setcookie('orig_utmcampaign', $_GET['utm_campaign'], 0, '/', $domains['cookie_domain']);
	}

}
add_action('init', 'set_ad_cookies');

Open in new window


What I'm finding though, is the cookies are not expiring,  A user will come back to the site a few days, or weeks later, and still have the same UTM codes stored in a cookie.

Is it possible, and if so how,  can i modify this function, so that it kills the cookie after say 2 hours.  That way, if the user comes back to the site after 2 hours, through one of our ads, it has the new UTM codes stored in a new fresh cookie.
PHPWordPress

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jon Imms

ASKER
Excellent. Thank you Sir.
Ray Paseur

Glad to help!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes