0

I am trying to find a way to easily remove the nofollow attribute ONLY for all the internal links on my site (WordPress).

How can I do that?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Are you aware of [wordpress.se]? There is already [Remove nofollow from specific internal links](https://wordpress.stackexchange.com/q/345770) there – Tomerikoo Aug 31 '21 at 08:16

1 Answers1

0

The snippet below will check the page content and comment fields so see if a string contains the site url and, if true, remove the any no-follow attribute if set. This is assuming the links are using an absolute path.

function remove_nofollow($string) {
    $internal = get_site_url();
    if ( str_contains( $internal, $string ) {
        $string = str_ireplace(' rel="nofollow"', '', $string);
    }
    return $string;
}
add_filter('the_content', 'remove_nofollow');
add_filter('comment_text', 'remove_nofollow');
WPhil
  • 1,056
  • 1
  • 7
  • 12