Skip to content

Instantly share code, notes, and snippets.

@amaudy
Created November 10, 2014 06:05
Show Gist options
  • Save amaudy/266ae7d3eb19c10a1b91 to your computer and use it in GitHub Desktop.
Save amaudy/266ae7d3eb19c10a1b91 to your computer and use it in GitHub Desktop.
hook wordpress $_GET
<?php
// Let say you want to do something with affid in http://mysite.com/about/?affid=1234
add_action('init', 'affiliate_redirect');
function affiliate_redirect() {
$varname = 'aff_id';
$weeks = '4';
if( isset($_GET[$varname]) && '' != $_GET[$varname] ) {
setcookie('wp_affiliate', $_GET[$varname], time()+648000*$weeks, '/');
$pageURL = 'http';
if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$url = preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$pageURL);
$last = $url[strlen($url)-1];
if ($last == '?') {
$url = substr_replace($url ,"",-1);
}
wp_redirect($url);
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment