wordpress hack – N77 Design | NickClaeboe.com https://www.nickclaeboe.com Atlanta Web, Graphic & Flyer Design • Design Portfolio & Rates for Nick Claeboe Custom Design Work Wed, 03 Mar 2021 14:33:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://www.nickclaeboe.com/wp-content/uploads/2016/05/favicon-60x60.png wordpress hack – N77 Design | NickClaeboe.com https://www.nickclaeboe.com 32 32 Carry and Pass Variables From Page to Page in WordPress – Google UTM Tags Vars https://www.nickclaeboe.com/2016/09/17/carry-pass-variables-page-page-wordpress-google-utm-tags-vars/ Sat, 17 Sep 2016 12:50:39 +0000 http://www.nickclaeboe.com/?p=406

Last week I posted about grabbing Google UTM tags from a url string and using that to hide or show specific content based on a variable. After that task, I kept running into another issue with Google UTM tags… Grabbing them from an incoming link, grabbing those variables, and passing each of them along in the url to each page that the visitor navigates.

At first I was searching for a plugin or a WordPress function to rewrite the url’s with my url string but kept getting nowhere.

After several hours of research and a day later, I found a javascript/php hack that produced the results that I was ultimately looking for. Yes, it could be prettier but it WORKS!

Locate your template footer.php file in your theme folder and place the following code just before the closing tag to grab Google UTM tags and pass them to each page of your WordPress site.

Here’s 5 of the most common Google UTM tags that we grab from the session:

utm_source
utm_medium
utm_camgpaign
utm_terms
utm_content


<?php $utm_source = isset($_GET['utm_source']) ? $_GET['utm_source'] : ""; ?>
<?php $utm_medium = isset($_GET['utm_medium']) ? $_GET['utm_medium'] : ""; ?>
<?php $utm_campaign = isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : ""; ?>
<?php $utm_term = isset($_GET['utm_term']) ? $_GET['utm_term'] : ""; ?>
<?php $utm_content = isset($_GET['utm_content']) ? $_GET['utm_content'] : ""; ?>


<script type="text/javascript">
jQuery(document).ready(function ($) {
$(function() {
$("a").attr('href', function(i, h) {
return h + (h.indexOf('?') != -1 ? "&utm_source=<?php echo $utm_source; ?>&utm_medium=<?php echo $utm_medium; ?>&utm_campaign=<?php echo $utm_campaign; ?>&utm_term=<?php echo $utm_term; ?>&utm_content=<?php echo $utm_content; ?>" : "?utm_source=<?php echo $utm_source; ?>&utm_medium=<?php echo $utm_medium; ?>&utm_campaign=<?php echo $utm_campaign; ?>&utm_term=<?php echo $utm_term; ?>&utm_content=<?php echo $utm_content; ?>");
});
});
});
</script>

The first 5 snippets of php code are how you grab the Google UTM tag variables from the incoming link.

As you can see, we are grabbing the variables from the session that being passing in from the link. We’re grabbing the dynamic variable and echoing that variable in the javascript below it.

The javascript is where the magic happens on page redirect.

When a visitor clicks on any internal site link, the variables are picked up from the session and echoed behind the hardcoded variable tokens.

Sidenote: the code can be optimized, but this down and dirty method works

 

]]>
Carry Variables From One Url To The Next PHP WordPress Tip Hack https://www.nickclaeboe.com/2016/09/10/carry-variables-one-url-next-php-wordpress-tip-hack/ Sat, 10 Sep 2016 13:28:57 +0000 http://www.nickclaeboe.com/?p=393

While using WordPress and running traffic to many WordPress sites, I discovered the need for variables to be passed from one page to the next as a visitor is navigating a WordPress site.

After doing some extensive research, I found a simple script that can be added to the theme functions.php file to carry dynamic variable from one page to the next as a visitor is on your site.

I’m my case, I wanted the “utm_source” variable “GA” be appended and carried at the end of my url. It would look something like this:

http://www.mywebsite.com/category/site/?utm_source=GA

What natively happens in WordPress , is that once a visitor clicks a link, the variable is dropped from the url and not carried/passed on to the next page. The following script, when placed in your theme functions.php file, will allow your WordPress theme template to keep that variable and pass it to the next page.

////////////////////////////////////////
///// pass vars page to page

function wprdcv_param_redirect(){
if( !is_admin() && !isset($_GET['CUSTOM ']) ){
$location = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$location .= "?CUSTOM =PARAM1 ";
wp_redirect( $location );
}
}
add_action('template_redirect', 'wprdcv_param_redirect');

/////////////////////////////////////////////////////////

Just replace CUSTOM and PARAM1 with the variables you’re using, in my case, CUSTOM = utm_source and PARAM1 = GA, in the above code and add it to your functions.php

Also, if you are needing to do this for more that one variable, simply duplicate the code and update the variables, but, be sure to create new function identifier as follows:

function wprdcv_param_redirectTWO(){
if( !is_admin() && !isset($_GET['CUSTOM2 ']) ){
$location = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$location .= "?CUSTOM2 =PARAM2 ";
wp_redirect( $location );
}
}
add_action('template_redirect', 'wprdcv_param_redirectTWO');

Be sure to test everything out and if you run into any errors, roll back your edits until you get it the way you want it.

Good luck!

 

]]>