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?
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:
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

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
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.