Sometimes, when you write a new post, you need to wirte links of other websites related with your post. The excessive amount of these links will be a serious problem for your website by the time. These links are known as external links. And the more the number of these links, the less your wbesite’s SEO quality. In order to prevent this serious link problem, you have to add rel=”nofollow” attribute to these links.
By default, WordPress does not automatically add a rel=”nofollow” attribute to external links within post content. If you want to do it, you have to do this by yourself.
There are some well-known wordpress plugins which put auto rel=nofollow attribute to external link within your post content. These wordpress plugins are Seo Smart Links plugin and Wp NoExternal Links plugin.
But, if you do not want to use any of these plugins, it is possible to do this by the help of a wordpress code snippet. All you need to do is, copy the following code below and paste it to your functions.php inside the loop.
WordPress Automatic NoFollow Code
By default, WordPress does not automatically add a rel=”nofollow” attribute to external links within post content. If you want to do it, you have to do this by yourself.
There are some well-known wordpress plugins which put auto rel=nofollow attribute to external link within your post content. These wordpress plugins are Seo Smart Links plugin and Wp NoExternal Links plugin.
But, if you do not want to use any of these plugins, it is possible to do this by the help of a wordpress code snippet. All you need to do is, copy the following code below and paste it to your functions.php inside the loop.
WordPress Automatic NoFollow Code
PHP Code:
add_filter('the_content', 'auto_nofollow');
function auto_nofollow($content) {
//return stripslashes(wp_rel_nofollow($content));
return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}
function auto_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
}
return $link;
}