WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a basic affiliation system plugin that for now, will do the following:

My issue lies with the first and last parts (reading the parameter and redirecting). I don't think that add_query_var is the correct way to go with, since I just want to track the referral and not se the parameter in a query. Any thoughts?

Edit: here's what I have until now:

add_action( 'template_redirect', 'test_redirect' );
function test_redirect() {
  global $wp;
  //cookie related stuff goes here
  $current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
  //strip the URL parameter aff_id from $current_url???
  wp_redirect( $current_url ); exit;

Edit: Working code, thanks to all of you and specifically @Max who helped a lot.

<?php 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") {
    } else {
    $url = preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$pageURL);
    $last = $url[strlen($url)-1];
    if ($last == '?') {
      $url = substr_replace($url ,"",-1);
} ?>
share|improve this question
Use PHP isset() to determine if $_GET['affid'] is set and PHP header('Location: http://mysite.com/about/') to redirect. – Max Yudin Feb 12 '13 at 10:09
That's what I was thinking, where do I hook the function? – Teo Maragakis Feb 12 '13 at 10:11
I think send_headers is appropriate. – Max Yudin Feb 12 '13 at 10:26
up vote 0 down vote accepted

This should work, though untested.

add_action('send_headers', 'affiliate_redirect');
function affiliate_redirect() {
    // it's possible to use 'if( !empty( $_GET['affid']) )'
    if( isset($_GET['affid']) && '' != $_GET['affid'] ) {
        if( empty($_COOKIE['affid']) )
            setcookie('affid', $_GET['affid'], time()+2592000, '/');
        header('Location: http://mysite.com/about/');
share|improve this answer
Just be sure to send the User to the Url he really requested, not just the hardcoded About-site. Also, I would use wp_redirect to handle the rest. – fischi Feb 12 '13 at 10:45
This did not work. 1. The cookie with the name "affid" is not created. 2. There's no redirection. In reality, I want it to redirect to the same page, without the affid appearing in the url. Take a look at my code. – Teo Maragakis Feb 12 '13 at 10:46
There was a syntax error. Feel free to change the location to your choice. And always remember, copying and pasting unverified code can lead to unexpected results down to permanent data loss. – Max Yudin Feb 12 '13 at 13:01
After your last revision I get the following issues: 1. links to the site's pages do not work (Server 404). 2. Cookie is not set. 3. calling the home url without parameters works, calling it with the affix gets a server 404. – Teo Maragakis Feb 12 '13 at 13:02
Any errors/warnings/notices? It works perfectly for me on any URL. – Max Yudin Feb 12 '13 at 13:16

Answer based on @MaxYudin answer

add_action( 'send_headers', 'wpse85540_affiliate_redirect' );
function wpse85540_affiliate_redirect()
    // Maybe even more elegant than simple $_GET, depending on if it was added:
    // $affid = get_query_arg( 'affid' );

    if (
        isset( $_GET['affid'] )
        AND ! empty( $_GET['affid'] ) 
        empty( $_COOKIE['affid'] ) AND setcookie(
            ,time() +4 * WEEK_IN_SECONDS
        $path = user_trailingslashit( '/about' );
        $scheme = is_ssl() ? 'https://' : 'http://';
        $url = is_multisite()
            ? network_site_url( $path, $scheme )
            : site_url( $path, $scheme );
        wp_redirect( $url ); // 302
share|improve this answer
Clicking on page links gives: The requested URL /index.php was not found on this server. Calling the home page with the affid parameter gives a blank page. But this is hardcoded to go to the about page, I need it to go to the same page and just lose the affid param. – Teo Maragakis Feb 12 '13 at 13:23
@TeoMaragakis Sorry, but I don't understand your comment. Also: Have you tried adding the affid as query arg as I commented in the beginning of the functioN? – kaiser Feb 12 '13 at 18:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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