[Resolved] How to add nofollow to affiliate links

Home Forums Support How to add nofollow to affiliate links

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1117581
    Chris

    Would you know how to automatically add nofollow to all affiliate links?

    I read this article, but it didn’t help. It would give me a PHP error.

    Any ideas on how to get this to work?

    #1117754
    David
    Staff
    Customer Support

    Hi there,

    what was the error?

    #1118476
    Chris

    Here’s the error I’m getting: http://prntscr.com/qhxlup

    Here’s what I added in the functions.php file: http://prntscr.com/qhxlhu

    #1118584
    David
    Staff
    Customer Support

    Looks like you got a bunch of errors in the code – if you compare your functions.php code to the original you will see a lot of the HTML has been escaped. You will need to correct the code so it matches that on the blog you got it from.

    #1118712
    Chris

    I don’t see anything missing (maybe because I don’t understand coding, haha). Could you correct the code:

    // Add no-follow tags for specific domain strings
    function add_nofollow( $content ) {
    
        $az_url = "amazon1.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
        $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
    $az_url = "amazon2.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
        $az_url = "amazon3.com";
        preg_match_all( '~<a.*>~isU', $content, $uri_match );
    
        for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
            if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
                 && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
                ) {
                $uri_change = trim( $uri_match[0][ $i ], ">" );
                $uri_change .= ' rel="nofollow">';
                $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
            }
        }
    
        return $content;
    }
    
    add_filter( 'the_content', 'add_nofollow' );
    #1118713
    Leo
    Staff
    Customer Support

    This is something you will need to check with the code author unfortunately.

    We can’t really help with custom functions that coming from an external source.

    #1119299
    Chris

    Thanks! Got it to work!

    #1119379
    Leo
    Staff
    Customer Support
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.