2

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.

3

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');
  • only 2 upvotes? (and one being from me) ts ts ts whats going on with stackoverflow? this guy should be accepted. This is legit. – Toskan Dec 23 '18 at 23:23
2

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.

0

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'); 
  • Thanks. But the above code makes the website give out redirection error. Any idea why? – sharky May 19 '15 at 11:29
  • I have tried things but the above code gives Redirection Loop. – sharky May 22 '15 at 6:35
0

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');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.