I am Looking to append URL Parameter to all URLs

I am trying to add URL parameter to all the URLs of WordPress website.

This is for tracking affiliate sales by tracking links.

E.g. I have put a link on another website

http://website.com/?aff=abc

When a person comes to my website via that link the parameter stays in the URL but as soon as the person goes to another page on my website the URL parameter is gone.

I want to keep the parameter in the URL no matter what page the user is on.

I found the following code that works but it works for only one parameter.

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

add_action('template_redirect', 'wprdcv_param_redirect');  

Is there a way to modify the above code to have any value in the parameter variable? Right now the value “abc” is fixed for the variable.

Solutions Collecting From Web of "I am Looking to append URL Parameter to all URLs"

I would recommend to save the affiliate related data in a cookie and use the values when needed. In my opinion it would be a saver way to keep the important information.

I don’t know if you still need the answer, but here’s working example for you.

Header.php

/*SAVE $_GET['aff'] TO COOKIE*/
if(!isset($_SESSION['aff']) and $_GET['aff']){
    $cookie_expire = time()+60*60*24*30;
    $_SESSION['aff'] = $_GET['aff'];
    setcookie('aff', $_SESSION['aff'], $cookie_expire, '/', '.'.$_SERVER['HTTP_HOST']);
}

Functions.php – if you need to pass one parameter

/*APPEND 'aff' PARAMETER IN URL THROUGH WHOLE SITE*/
function wprdcv_param_redirect(){
    if(isset($_COOKIE['aff']) and !$_GET['aff']){
        $location = esc_url(add_query_arg('aff', $_COOKIE['aff']));
        wp_redirect($location);
    }
}
add_action('template_redirect', 'wprdcv_param_redirect');

OR: Functions.php – if you need to pass array of parameters

/*APPEND 'aff' PARAMETER IN URL THROUGH WHOLE SITE*/
function wprdcv_param_redirect(){
    if(isset($_COOKIE['aff']) and !$_GET['aff']){
        $location = esc_url(add_query_arg(array('aff' => $_COOKIE['aff'], 'parameter_key' => 'parameter_value', 'parameter_key_two' => 'parameter_value_two')));
        wp_redirect($location);
    }
}
add_action('template_redirect', 'wprdcv_param_redirect');

Look into using add_query_arg() (documented here) to attach the query parameter(s) to the URL. This (below) is untested, and there is probably a nicer way to get $baseUrl, but hopefully this example will point you in the right direction.

function wprdcv_param_redirect(){
  if( !is_admin() ){
    $baseUrl = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $location = add_query_arg( $_SERVER['QUERY_STRING'], '', $baseUrl );
    wp_redirect( $location );
  }
}
add_action('template_redirect', 'wprdcv_param_redirect'); 

You can use the below function which makes use of parse_url and http_build_url:

function wprdcv_param_redirect() {
    //Prevent infinite loop
    if ($_GET["redirected"] == "1") return;

    $url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    parse_str($url_parts['query'], $params);

    $params["redirected"] = "1";
    foreach (array_keys($_GET) as $n) {
        $params[$n] = $_GET[$n];
    }

    // Note that this will url_encode all values
    $url_parts['query'] = http_build_query($params);
    wp_redirect(http_build_url($url_parts));
    exit;
}
add_action('template_redirect', 'wprdcv_param_redirect');