2

I have a form in all my pages, and it has an hidden field named ref

If the user land at www.example.com/1/ref=fb_ad and fill in the form in this page - the form catches the URL parameter.

However, if the user continue to www.example.com/2 the URL parameter doesn't go with him, so if he fills the form in another page other than the landing page - the ref hidden field remain empty.

I'm looking for an option to save that url parameter, so the hidden field will catch in even if the user moved to another page

1 Answer 1

1

1. Track Affiliate via Cookie

To track the affiliate source you can use a Cookie for internal tracking. In that case you only need to modify external links.

add_action( 'plugins_loaded', 'so265278_affiliate_check' );

function so265278_affiliate_check() {
    $is_affiliate = ! empty( $_GET['ref'] );
    if ( $is_affiliate ) {
        setcookie( 'affiliate', $_GET['ref'], MONTH_IN_SECONDS );
    }
}

Now you can always check the affiliate source in your theme/plugins like this:

function get_affiliate_source() {
    if ( ! empty( $_GET['ref'] ) ) { return $_GET['ref']; }
    if ( ! empty( $_COOKIE['affiliate'] ) ) { return $_COOKIE['affiliate']; }
    return false;
}

// ...
echo 'Affiliate source: ' . get_affiliate_source();

2. Replace links on page

Modifying (the external) links is a bit tricky. If you need to modify ALL links then you can use the shutdown hook and use a regex to replace all links.

ob_start(); // I think WP does ob-buffering by itself. You can try to remove this line...

add_action( 'shutdown', 'so265278_shutdown' );

function so265278_shutdown() {
    $html = '';

    while (ob_get_level() > 1) {
        $html .= ob_get_clean();
    }

    // replace the links here...

    echo $html;
}

Of course you need to have a good regex to replace all the correct links on your page.

$regex = '#(<a.*?href=["\'])(.*?)(\2.*?>)#i';
$html = preg_replace_callback( $regex, '_modify_link', $html );

function _modify_link( $match ) {
    $url = add_query_arg( 
        array( 'ref' => get_affiliate_source() ), 
        $match[2] 
    );

    // Return the full `<a href...>` tag:
    return $match[1] . $url . $match[3];
}

Better solution (different approach)

The above shutdown handler is not the most efficient solution, as it will parse every request, also on wp-admin pages. It would be better if you have a specific function to create the external links with the correct markup.

Linke this:

function show_external_link( $url, $label ) {
    $affiliate = get_affiliate_source();

    if ( $affiliate ) {
        $url = add_query_arg( 
            array( 'ref' => get_affiliate_source() ), 
            $match[2] 
        );
    }

    printf(
        '<a href="%1$s" target="_blank" rel="nofollow" class="external">%2$s</a>',
        esc_url( $url ),
        $label
    );
}

// In your theme/plugins
<?php show_external_link( 'http://www.facebook.com/', 'Facebook' ); ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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