Useful 'hack' for Amazon (and other) affiliates - Nofollow & Open in New Tab Automatically

andreint

BuSo Pro
Joined
Oct 20, 2014
Messages
167
Likes
241
Degree
1
Hey,

just wanted to leave this script here in case someone finds adding nofollow and target blank attributes to affiliate links time-consuming and frustrating.

This script will automatically make all your outbound links open in a new window + all your Amazon affiliate links will automatically be labeled as nofollow. You can just paste the affiliate link in your content and everything will be handled for you.

Add this to your functions.php file:

Code:
function cdx_handel_external_links() {
    ?>
<script type="text/javascript">
( function( $ ) {

    $("a[href^=http]").click(function(){
      if(this.href.indexOf(location.hostname) == -1) {
         $(this).attr({
            target: "_blank"
         });
      }
    })

   //Add Nofollow
   $("a").each(function(){
    if(this.href.indexOf('amazon.com') >0 ){
        $(this).attr({
            rel: "nofollow"
         });
    }
   });

} )( jQuery );
</script>
   <?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
 
Last edited:
I was looking at something like this yesterday but filtering the_content() through PHP on Wordpress.

Remember though, this code you posted is applied after the page has loaded. It's not in the source code itself. So depending on if Google is executing your Javascript before it wonders if your affiliate links are nofollow or not, it still may not be the best fix. PHP would change the source code itself.

You could also do a MySQL search and replace in the database to add rel=nofollow and target=_blank
 
Here are two PHP "find and replace" snippets that get the job done for me:
  • Nofollow all amazon.com and amzn.to links
  • Add target="_blank" and rel="noopener" to all content links
I found these snippets online and tweaked them until things were working. I'm not a developer. Maybe someone with experience can chip in on how to make this code prettier / more robust.

Code:
// Add target="_blank" and rel="noopener" to all content links

function open_links_in_new_tab($content){
    $pattern = '/<a(.*?)?href=[\'"]?[\'"]?(.*?)?>/i';

    $content = preg_replace_callback($pattern, function($m){
        $tpl = array_shift($m);
        $hrf = isset($m[1]) ? $m[1] : null;

        if ( preg_match('/target=[\'"]?(.*?)[\'"]?/i', $tpl) ) {
            return $tpl;
        }

        if ( trim($hrf) && 0 === strpos($hrf, '#') ) {
            return $tpl; // anchor links
        }

        return preg_replace_callback('/href=/i', function($m2){
            return sprintf('target="_blank" rel="noopener" %s', array_shift($m2));
        }, $tpl);

    }, $content);

    return $content;
}

add_filter('the_content', 'open_links_in_new_tab', 999);


// Nofollow amazon.com and amazon.to links

add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
 
function my_nofollow($content) {
    return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
 
function my_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
 
    if(strpos($link,'#') ==! false){
        return $link;
    }
    
    if ((strpos($link, 'nofollow') === false) && (strpos($link, 'amazon.com') ==! false)) {
        $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow noopener" $1', $link);
    }
    
    if ((strpos($link, 'nofollow') === false) && (strpos($link, 'amzn.to') ==! false)) {
        $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow noopener" $1', $link);
    }
    
    elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow noopener"', $link);
    }
    
    return $link;
}
 
Back