Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a wordpress website for a client.

MY AIM:

Website will have links to products from various websites. Those links are actually affiliate links for website and services. Those link are needed to be converted into http://www.example.com/go/123 or something like that and when clicked they redirect to the actual link. The next requirement would be to tracking the number of outgoing links.

Can you guys, wordpress expert let me about any plugin that would do the job, or even one of the thing that I want.

I have also searched this website as well as plugins directory on wordpress.org, but have not found any suitable plugin.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted
+25

I would let something like Google Analytics do the tracking for you with something like event tracking. Look into Yoast's Google Analytics plugin, it will let you do this fairly easily.

Otherwise, something like Redirection would be good for creating redirects. And, of course, you can see how many times each was clicked. I've used the plugin for years now and it's pretty good.

If you want to do something automatically, You'd need to create a custom rewrite rule and query variable:

<?php
add_action( 'init', 'wpse36168_add_rewrite_rule' );
/**
 * Add our rewrite rule
 */
function wpse36168_add_rewrite_rule()
{
    add_rewrite_rule(
        '^go/(.*?)$',
        'index.php?go=$matches[1]',
        'top'
    );
}

add_filter( 'query_vars', 'wpse36168_add_go_var' );
/**
 * Tell WP not to strip out or "go" query var
 */
function wpse36168_add_go_var( $vars )
{
    $vars[] = 'go';
    return $vars;
}

Then hook into template_redirect to throw people to the other URL if that query variable is caught.

<?php
add_action( 'template_redirect', 'wpse36168_catch_external' );
/**
 * Catch external links from our "go" url and redirect them
 */
function wpse36168_catch_external()
{
    if( $url = get_query_var( 'go' ) )
    {
        wp_redirect( esc_url( $url ), 302 );
        exit();
    }
}

And finally, you can hook into the_content to auto replace all external links with the yoursite.com/go/someurlhere.com/asdf links.

<?php
add_filter( 'the_content', 'wpse36168_replace_links', 1 );
/**
 * Replace external links with our "go" links
 */
function wpse36168_replace_links( $content )
{
    $content = preg_replace_callback( '%<a.*?href="(.*?)"[^<]+</a>%i', 'wpse36168_maybe_replace_links', $content );
    return $content;
}

function wpse36168_maybe_replace_links( $matches )
{
    if( ! preg_match( sprintf( '#^%s#i', home_url() ), $matches[1] ) )
    {
        $url = $matches[1];
        // http:// we'll add it later
        $url = str_replace( 'http://', '', $url );
        $url = sprintf( '/go/%s', $url );
        return str_replace( $matches[1], home_url( $url ), $matches[0] );
    }
    else
    {
        return $matches[0];
    }
}

Here's all of that mess as a plugin.

share|improve this answer
    
Yup, something like Google analytics or CloudFlare's tracking functions should do the trick. –  Teknophilia Dec 14 '11 at 18:21
    
Using this how much feasible it is to be managing like 3000 links –  Nikhil Dec 15 '11 at 6:58
    
The stuff I wrote or redirection? What I wrote will work for however many links -- but there's no tracking. Using redireciton or something like that would work for however many links, but you'd have to add each manually. So in a sense, it would be fine, but it might be more work for you to set all of those up. –  chrisguitarguy Dec 15 '11 at 16:34
add comment

Your Answer

 
discard

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.