wordpresspreg-replacenofollow

NoFollow Rel="sponsored" pre_replace


I use wordpress, I've added a bit of code to my functions.php to automatically all links as no-follow that don't meet certain conditions.

Currently, if the link examplesite.com or examplesite2.com mark as dofollow else set to nofollow

The code that I use to achieve that (which works perfectly) is:

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

However, I want to add an additional condition.

If the site is examplesite or examplesite2.com mark it as rel="follow"

elseif

if it's examplesite3.com or examplesite4.com mark it as rel=”sponsored”

else (if it doesn't meet either of those conditions return

no Follow instead.

could someone help me with this? I tried adding

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[1], "https://examplesite3.com") === false && strpos($m[3], "https://examplesite4.com") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[3].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

But it created a loop and didn't work. Thank you in advance for any ideas!

and I also tried this one, altering the elseif but it doesn't seem to get past the 2nd condition.

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[3], "https://www.bluehost.com") === false && strpos($m[3], "https://amzn.to") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
elseif (strpos($m[3], "https://www.examplesponsor.com") === false && strpos($m[3], "https://www.examplesponsor2.com") === false && strpos($m[3], "https://examplesponsor3.com") === false)
return '<a href="'.$m[3].'" rel="sponsored" target="_blank">'.$m[4].'</a>';
        else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

Solution

  • Try it like this:

    function add_nofollow_content($content) {
      $content = preg_replace_callback(
        '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
        function($m) {
          if ((strpos($m[1], "https://www.examplesite.com") !== false) ||
              (strpos($m[1], "https://www.examplesite2.com") !== false)) {
            return '<a href="'.$m[1].'" rel="follow" target="_blank">'.$m[2].'</a>';
          } elseif ((strpos($m[1], "https://www.examplesite3.com") !== false) ||
                    (strpos($m[1], "https://www.examplesite4.com") !== false)) {
            return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[2].'</a>';
          } else {
            return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
          }
        },
        $content);
      return $content;
    }
    add_filter('the_content', 'add_nofollow_content');
    

    This checks if either of the two URLs is found using strpos and returns the appropriate rel= for each case. For all other URLs, it will return rel="nofollow". In case you have to define more URLs, make sure to logically || them as well.

    For a seriously longer list of URLs, I'd change the code structure to do an array mapping of URL → rel type and loop over the entries matching each one against the match.