How to add automatically no follow to links ?

kurosaki4d

Power Member
Joined
Apr 24, 2018
Messages
694
Reaction score
83
Hello,

I have some cloaking affiliate links to which i would like to add the attributes of nofollow

However, i'm looking for a way in coding that will allow me to target links that contain specific string, like /go/brand

While searching, i found this code somewhere, but it didn't work for me in functions.php, so i was hoping some of you guys might provide with something that might do the job for me. That would be much appreciated !

Thank you so much in advance !

PHP:
add_filter( 'the_content', 'my_string_replacements');
function my_string_replacements ( $content ) {

if ( ! is_admin() && is_main_query() ) {
    return preg_replace('/<a(.*href=".*\/go\/pluto\/?".*)>/', '<a$1 rel="nofollow">', $content ); }
    return $content;
}
 
Back
Top