We help IT Professionals succeed at work.
Get Started

Set a time limit on Wordpress Cookie function.

Jon Imms
Jon Imms asked
Last Modified: 2017-04-20
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.
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE