Wordpress Cookie function help

Jon Imms
Jon Imms used Ask the Experts™
on
Hi, I'm having a problem with one of my WordPress Functions.

I have a function, which when somebody comes to our website, with a utm_source, it stores it in a cookie. If I then click on an apply button, it will then append that utm_source value to the application link.

I've got some apply now buttons in my pages (the_content) and some outside, such as in my header.   So testing, I goto this link, with a utm code attached.

http://c643d67b.ngrok.io/?utm_source=testing

If I hover over or click [DRIVERS APPLY] nothing is appended. But, if you scroll down the page, you will see 2 other buttons just above the footer. These are in my page content, and if you hover over, or click, it will append the utm_source value to the url.  So it works in the_content, or if i have it in a widget_text.

If it's not in a widget, or in the content, how could i target it, if it's anywhere other than in the_content

This is the code i'm using. -

/*------------------------------------*\
   Add Cookies
/*------------------------------------*/

function set_ad_cookies() {
	
  $vars = array('utm_source', 'utm_campaign', 'utm_media');
  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_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');

function replace_links($text) {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$addCookiesToLink = "";
	if (strlen($url[1]) > 1) {
		$text = str_replace('https://intelliapp2.driverapponline.com/c/transwood', 'https://intelliapp2.driverapponline.com/c/transwood?' . $url[1], $text);
	}
	else { //no parameters in URL. Cookies?
		if(isset($_COOKIE['orig_utmsource'])) {
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utmsource'];
		}
		elseif(isset($_COOKIE['orig_utm_source'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utm_source'];
		}
		if(isset($_COOKIE['orig_utmmedium'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_medium=" . $_COOKIE['orig_utmmedium'];
		}
		if(isset($_COOKIE['orig_utm_media'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_media=" . $_COOKIE['orig_utm_media'];
		}
		if(isset($_COOKIE['orig_utmcampaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utmcampaign'];
		}
		elseif(isset($_COOKIE['orig_utm_campaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utm_campaign'];
		}
		
		if(strlen($addCookiesToLink) > 0) {
			$text = str_replace('https://intelliapp2.driverapponline.com/c/transwood', 'https://intelliapp2.driverapponline.com/c/transwood?' . $addCookiesToLink, $text);
		}
		
		//echo "test1" . $_COOKIE['orig_utm_campaign'] . " 2:" . $_COOKIE['orig_utm_source'] . " 3:" . $_COOKIE['orig_utmcampaign'] . " 4:" . $_COOKIE['orig_utmmedium'] . " 5:" . $_COOKIE['orig_utmsource'];
		
	}
	return $text;
}
add_filter('widget_text', 'replace_links');
add_filter('the_content', 'replace_links');

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
the_content refers to the post text retrieved from the DB if you have content outside of this then you are not going to be able to filter it using the_content.

One option would be to buffer the output of the page (ob_start()) by adding a hook either to wp_head or after_setup_theme hooks and then calling ob_get_clean() on the wp_footer or shutdown hooks.

By adding a callback function to the ob_start() call you can then modify the links in the callback.

The other option is to amend the links using JavaScript.

Using jQuery it would be something like this

jQuery(function($) {
    var cookies = getCookies();
    var cookieStore = [];
    if (cookies['orig_utmsource']) {
        cookieStore.push('utm_source=' + cookies['orig_utmsource']); 
    }
    // ... rest of tests here
    var cookieStr = cookieStore.join('&');

    $('a').each(function(i,e) {
         // As an example - you can flesh out as required
          e.href = e.href + '?' + cookieStr;           
    }
});

function getCookies()
{
  var cookies = {};
  
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var cookie = ca[i].split('=');
    cookies[cookie[0].trim()] = cookie[1].trim();
  }
    return cookies;
}

Open in new window

Jon ImmsWeb Developer

Author

Commented:
I used the  ob_start() method and worked great. Thank you.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial