Archived topic
How to add nofollow to affiliate links
7 replies · Started by Chris on December 30, 2019
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?
Hi there,
what was the error?
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
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.
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' );
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.
Thanks! Got it to work!
No problem :)