What is Nofollow?
The rel="nofollow" attribute is an HTML link attribute used to instruct search engines not to follow a specific link or pass ranking weight (PageRank) through it. This prevents the link from being counted as an endorsement of the linked site, helping to manage your site's SEO equity.
Why Add Nofollow to External Links in WordPress?
Adding nofollow to external links in your WordPress posts is a recommended SEO practice. It prevents your site's authority from being transferred to external sites you don't control and helps avoid association with potentially low-quality sites.
Code Example
A standard link with the nofollow attribute looks like this:
<a href="https://example.com/" rel="nofollow">Example Link</a>
How to Automatically Add Nofollow to External Links in WordPress
You can automatically add rel="nofollow" to all external links in your post content by adding code to your theme's functions.php file.
Method 1: Basic Implementation
Add the following code to the end of your active theme's functions.php file:
function auto_nofollow_external_links($content) {
$site_host = parse_url(home_url(), PHP_URL_HOST);
return preg_replace_callback('/<a([^>]+?)href="([^"]+?)"([^>]*?)>/i', function($matches) use ($site_host) {
$link = $matches[0];
$url = $matches[2];
$url_host = parse_url($url, PHP_URL_HOST);
if ($url_host && $url_host !== $site_host && stripos($link, 'rel="nofollow"') === false) {
if (stripos($link, 'rel="') !== false) {
$link = str_ireplace('rel="', 'rel="nofollow ', $link);
} else {
$link = str_replace('<a', '<a rel="nofollow"', $link);
}
}
return $link;
}, $content);
}
add_filter('the_content', 'auto_nofollow_external_links');
Method 2: Robust Version (Recommended)
This version uses DOMDocument for more reliable HTML parsing:
function auto_nofollow_external_links_robust($content) {
if (empty($content) || !class_exists('DOMDocument')) return $content;
$internal_host = parse_url(home_url(), PHP_URL_HOST);
$dom = new DOMDocument();
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$href = $link->getAttribute('href');
if ($href) {
$href_host = parse_url($href, PHP_URL_HOST);
if ($href_host && $href_host !== $internal_host) {
$rel = $link->getAttribute('rel');
if (stripos($rel, 'nofollow') === false) {
$link->setAttribute('rel', trim($rel . ' nofollow'));
}
}
}
}
return $dom->saveHTML();
}
add_filter('the_content', 'auto_nofollow_external_links_robust', 999);
Usage and Notes
- Placement: Add either code snippet to the end of your active theme's
functions.phpfile. - Effect: After saving, external links in new or updated posts will automatically receive the
rel="nofollow"attribute. Internal links are unaffected. - Safety: Method 2 (DOMDocument) is recommended for better HTML handling.
- Plugin Alternative: SEO plugins like Yoast SEO or Rank Math often include similar nofollow automation features.
- Testing: After adding the code, publish a post with an external link and check the page source to confirm the attribute is added.
Using these methods helps manage your site's SEO link equity effectively.