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

Jon ImmsWeb DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jon ImmsWeb DeveloperAuthor Commented:
I used the  ob_start() method and worked great. Thank you.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.