Links are commonly used within posts on WordPress to point the user to an external location that may be of interest to them. Sometimes an external link may point to a questionable website or one with no reputable value. When we link to an external website, search engines pass a small part of link authority from your website to the linked website. We don't want to give authority to all websites that we may link to because it may hurt our search ranking later on down the road. To avoid this issue, we can add the link attribute rel=”nofollow” to the link.
<a href="https://example.com" rel="nofollow">Visit Example Website</a>
That is easy enough, but what if you have a lot of external links that you need to add the nofollow attribute to? That is where this handy little WordPress snippet comes into play. It will automate the process of adding the attribute to all external links. I have also taken the time to add multiple options within the snippet that you can use depending on how you want to handle the links.
The snippet will also detect if there are currently any link rel attributes added already and instead of removing them, it will add the nofollow attribute to them.
You can add this snippet to your child theme's functions.php file, but I recommend installing a really cool plugin called Code Snippets. Code Snippets is an easy, clean and simple way to run code snippets on your site. It removes the need to add custom snippets to your theme’s functions.php file by providing a GUI interface in your Admin Dashboard.
function strposa( $haystack, $needle, $offset=0 ) { if ( !is_array( $needle ) ) $needle = array($needle); foreach ( $needle as $query ) { if ( strpos( $haystack, $query, $offset ) !== false ) return true; } return false; } function domain_in_whitelist( $content, $domains ) { $doc = new DOMDocument(); $doc->loadHTML( $content ); $xpath = new DOMXPath( $doc ); $nodeList = $xpath->query( '//a/@href' ); for ( $i = 0; $i < $nodeList->length; $i++ ) { $url = parse_url( $nodeList->item( $i )->value )['host']; if ( strposa( $url, $domains ) ) { return true; } } } function add_nofollow_callback( $content, $whitelist_domains="" ) { return preg_replace_callback('|<a (.+?)>|i', function( $matches ) use ( $whitelist_domains ) { $text = $matches[1]; $site_link = get_bloginfo( 'url' ); if ( strpos( $text, $site_link ) || domain_in_whitelist( $matches[0], $whitelist_domains ) ) { return "<a $text>"; } if ( strpos( $text, "rel=" ) ) { if ( strpos( $text, "nofollow" ) === false ) { $text = preg_replace( '/rel="(.*)"/isU', 'rel="$1 nofollow"', $text ); return "<a $text>"; } else { return "<a $text>"; } } else { if ( strpos( $text, "nofollow" ) === false ) { $text = preg_replace( '/href=(.*)/i', 'href=$1 rel="nofollow"', $text ); return "<a $text>"; } else { return "<a $text>"; } } return "<a $text>"; }, $content); } function add_rel_nofollow( $content ) { /* Uncomment the code by removing the "//" in front of each option that you would like to use. If you would like to whitelist certain domains from being modified to use the rel nofollow attribution, you can add the domains to the $whitelist_domains array below. */ // Add Domains to be Whitelisted $whitelist_domains = array( 'example.com', 'example3.com' ); // Whitelist Single Post // if ( is_single( '' ) ) { // return $content; // } // Whitelist Multiple Posts // if ( is_single( array( '', '' ) ) ) { // return $content; // } // Whitelist Single Category // if ( in_category( 'category-slug' ) ) { // return $content; // } // Whitelist Multiple Categories // if ( in_category( array( 'category-slug', 'category2-slug' ) ) ) { // return $content; // } // Add NoFollow to Single Post except for Domains Whitelisted // if ( is_single( '36007' ) ) { // return add_nofollow_callback( $content, $whitelist_domains ); // } else { // return $content; // } // Add NoFollow to Multiple Posts except for Domains Whitelisted // if ( is_single( array( '36007', '35334' ) ) ) { // return add_nofollow_callback( $content, $whitelist_domains ); // } else { // return $content; // } // Add NoFollow to Single Category except for Domains Whitelisted // if ( in_category( 'category-slug' ) ) { // return add_nofollow_callback( $content, $whitelist_domains ); // } else { // return $content; // } // Add NoFollow to Multiple Categories except for Domains Whitelisted // if ( in_category( array( 'category-slug', 'category2-slug' ) ) ) { // return add_nofollow_callback( $content, $whitelist_domains ); // } else { // return $content; // } // Add to All External Links except for Domains Whitelisted return add_nofollow_callback( $content, $whitelist_domains ); } add_filter( 'the_content', 'add_rel_nofollow' );
Test Content
<a href="https://example.com">example.com</a> <a href="https://www.example.com">www.example.com</a> <a href="https://www.example2.com">www.example2.com</a> <a href="https://test.example3.com">test.example3.com</a> <a href="https://www.example4.com">www.example4.com</a> <a href="https://www.example5.com">www.example5.com</a>
Output
<a href="https://example.com">example.com</a> <a href="https://www.example.com">www.example.com</a> <a href="https://www.example2.com" rel="nofollow">www.example2.com</a> <a href="https://test.example3.com">test.example3.com</a> <a href="https://www.example4.com" rel="nofollow">www.example4.com</a> <a href="https://www.example5.com" rel="nofollow">www.example5.com</a>