You Are Here: Home » How-To » Wordpress

WordPress – Automatically Add Nofollow To External Links Without Plugin [How-To]

By Debjit on March 22nd, 2015 
Advertisement

Adding Nofollow to external links in WordPress without using any plugin is very simple. But before that, some basic seo jargons - building links is one of pillars of search engine optimisation and infact it is so important that at times webmasters turn to adopting shady methods of building links to their websites just in order to get higher search ranking and traffic. rel=nofollow is an important property that determines whether or not a link on a page of your website should be followed across and crawled by the search engine.

rel nofollow in WordPress

rel nofollow in WordPress

In WordPress, when you write a blog post and insert one or two links in it, these links are dofollow by default and you have to manually ad nofollow to them each time you link to an external site. Doing this on a daily basis and repetetively might become very cumbersome at one point of time. Although there are plugins like WP External Links that take care of automatically adding rel="nofollow" to any external domain or the domains that you specifically choose to nofollow, but I prefer not using a plugin.

In this post we will show you that how without using a plugin you can automatically add rel="nofollow" to all links in your blog posts that link to an external domain, you can also choose which domains to skip.

Open the functions.php file in your WordPress Blog's theme folder (inside wp-content) and add the following code snippet towards the end of it:

/**
* add nofollow to links
*/
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "YOUR_DOMAIN_HERE") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

Replace YOUR_DOMAIN_HERE in the above code snippet with the domain name of your website. We do this to avoid adding rel="nofollow" to the internal links on your website. Now, in order to prevent adding rel="nofollow" to any other domains, use the code snippet below:

 

/**
* add nofollow to links
*/
function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "YOUR_DOMAIN_HERE") === false && strpos($m[1], "YOUR_DOMAIN_HERE") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');


DO let us know if you liked this post. In case you are stuck, just leave a comment below.

Advertisement







WordPress – Automatically Add Nofollow To External Links Without Plugin [How-To] was originally published on Digitizor.com on July 5, 2014 - 7:45 am (Indian Standard Time)