I’d like to make all the links in posts on one of my sites to be with rel=”nofollow” on links inside posts. I wasn’t able to find a plugin that did the job except for WP-NoExternalLinks. It also didn’t work, unless I used it’s dooms day option:
“Mask ALL links in document (can slow
down your blog and conflict with some
cache and other plugins. Please use it
on your own risk.”
But when I use it, it also puts nofollow on my blogroll links (which I would have preferred to keep alive.)
Any suggestion what might be causing this? or how to resolve it?
Thanks.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
you can add a filter in your functions.php
add
// Nofollow in content
add_filter('the_content', 'my_nofollow');
function my_nofollow($content) {
//return stripslashes(wp_rel_nofollow($content));
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_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;
}
Method 2
Have you tried using jQuery to add the nofollow? I think, if I understand your post correctly, you ONLY want to stop the follow on external links, but follow internal links correct?
$(function() {
$("a[href^=http://]").attr("rel","nofollow");
});
Method 3
/**
* 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 noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');
This function uses the regex to find the http and other links and then also skips adding nofollow to links that have your domain name in it. More details on http://digitizor.com/2014/07/05/add-nofollow-external-wordpress/. More details are included on that page about adding other domains as well.
Method 4
There’s the plugin Rel Nofollow now, which does what needed here.
Quoting its description:
When a post is saved, the plugin adds the rel=”nofollow” attributes to post links. The plugin also provides an apt checkbox to exclude a post from plugin’s action.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0