Add rel=”nofollow noindex noopener” string to all HTML elements in content.

Last update: 17 January 2024
To improve the site’s position in search results, it is recommended to block all external links leading from your site from indexing (set a ban on indexing by the search robot).
Add rel=”nofollow noindex noopener” string to all HTML  elements in content.

The function automatically closes all external links in the post text, adds an attribute that prohibits indexing of the link ‘” rel=”nofollow noindex noopener”‘, and adds an attribute to open the link in a new window ‘target=”_blank”‘.

Add code in your file function.php

Tested:
WordPress 6.4.2
PHP 7.4

/**
 * Add `rel="nofollow nofollow noindex noopener"` string to all HTML <a> elements in content.
 *
 * @since    1.0.1
 */
add_filter( 'the_content', 'wphaf_wp_rel_nofollow' );
   
function wphaf_wp_rel_nofollow( $content ) {

  $content = wp_unslash( wp_rel_nofollow( $content ) );
  $content = str_replace( 'rel="nofollow"', 'target="_blank" rel="nofollow noindex noopener"', $content );

  return $content;

}