string replace

Jon Imms
Jon Imms used Ask the Experts™
on
Hey there, I'm having a problem with a string replace not working as it should.

Basically, I've got a button link on my home page - https://cretecarrier.com/ - (Hunt Flatbed APPLY)
The link is https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY

Now, I also have a tracking function which stores UTM codes as cookies. I also use it to append it to url's.  

So an example, i click a google add which takes me to our websites.  https://cretecarrier.com/?utm_source=the-big-test

In theory, what should happen is the link - https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY . should change to https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test . BUT it is changing to https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test?r=CreteCarrier-Homepage-HUNT-APPLY

How can i, if i come into the site through an add with a UTM code, change the url from
- https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY 
TO
- https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test

Here is my current code.
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_linkshunt_home($text) {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$addCookiesToLink = "";
		if (strlen($url[1]) > 1) {
		$text = str_replace('https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY', 'https://intelliapp2.driverapponline.com/c/hunt?' . $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) {
				//echo $addCookiesToLink;
					$text = str_replace('https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY', 'https://intelliapp2.driverapponline.com/c/hunt?' . $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_linkshunt_home');
add_filter('the_content', 'replace_linkshunt_home');

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:
Just to clarify your question can be simplified to this?
How can i, if i come into the site through an add with a UTM code, change the url from
- https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY 
TO
- https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test
There are several options you can use.
My question is - what were you expecting.
Let's look at it like this
IN THIS               : https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY
SEARCH FOR THIS       : https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY
AND REPLACE WITH THIS : https://intelliapp2.driverapponline.com/c/hunt?

Open in new window

Or to put it another way
Replace AB in ABC with DE => DEC

I am not clear on what is coming in though - where is the utm_source=the-big-test coming from?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You did get a response to your question.

I submitted a post with questions clarifying your requirement. You never responded to that.

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