We help IT Professionals succeed at work.

string replace

Jon Imms
Jon Imms asked
on
117 Views
Last Modified: 2019-03-12
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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You did get a response to your question.

I submitted a post with questions clarifying your requirement. You never responded to that.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.