PHP Function , converted from WordPress

Hey,  

I've been using a function in Wordpress which works great. It stores cookies from ads, and appends UTM's to the end of links, so we can track sources.

My friend has a pure PHP, MySQL website, and we want to try and convert this function we have in WordPress into this PHP website.

This is the function we are using in Wordpress,  It's on all pages of the website.   How can i convert this to a php function.

function replace_links($text) {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$addCookiesToLink = "";
	if (strlen($url[1]) > 1) {
		$text = str_replace('intelliapp2.driverapponline.com/c/cretec', 'intelliapp2.driverapponline.com/c/cretec?' . $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'];
		}
		elseif(isset($_COOKIE['orig_utmmedium'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_medium=" . $_COOKIE['orig_utmmedium'];
		}
		elseif(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('intelliapp2.driverapponline.com/c/cretecarrier', 'intelliapp2.driverapponline.com/c/cretecarrier?' . $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?
 
Julian HansenCommented:
This is a PHP function. WordPress is written PHP.

There aren't even any WP specific function calls in that code.

What the code does is takes content before it is sent to the browser and updates links with parameters either obtained from the calling URL or from a variety of cookies.

If you want to implement this on your friend's site we would need to know how he is sending content to the browser. Before he outputs his page content he will need to call this function with that content i.e

$content =  get_friends_content_from_wherever_it_is_stored_or_generated();
$actual_content = replace_links($content);
// NOW THE OUTPUT WILL CONTAIN THE UPDATED LINKS
echo $actual_content();

Open in new window

I am assuming the link text you will be searching for remains the same i.e
ntelliapp2.driverapponline.com/c/cretec

Open in new window

Otherwise you will have to update that - otherwise you can use the function as is.

The add_filter() function calls at the bottom of the code you posted are just for linking that function to WP output - they have no bearing on the actual function itself and can be ignored.
0
All Courses

From novice to tech pro — start learning today.