Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Wordpress Cookie function help

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

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jon Imms

ASKER

I used the  ob_start() method and worked great. Thank you.
You are welcome.