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?


4 Answers 4


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;
  • Hi Mauri, thanks for trying. I just used it on the site and it didn't work. Any idea why it wouldn't work? I am using this theme: wordpress.org/extend/themes/magazine-basic
    – Tal Galili
    Commented May 5, 2011 at 12:53
  • @Tal: didn't work at all? or just on archive or front page? if so try adding add_filter('the_excerpt', 'my_nofollow');
    – Bainternet
    Commented May 5, 2011 at 14:31
  • Be aware that regex and HTML are a tricky combination.
    – Geert
    Commented May 5, 2011 at 15:09
  • Hi Bainternet. I just tried it also, and it also didn't work. When I open the single.php, all I get is <?php get_header(); ?> <?php get_template_part( 'loop', 'single' ); ?> <?php get_footer(); ?>. inside loop.php, there is the line - <?php if(!is_single()) if(function_exists('pagination')) { pagination(); } ?> And the "pagination" function is in functions.php, the question is what to do with it :)
    – Tal Galili
    Commented May 5, 2011 at 16:32

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() {
  • I don't think adding nofollow with Javascript will help, since it is there to indicate something to search spiders, and they don't execute Javascript code.
    – Jan Fabry
    Commented May 5, 2011 at 14:08
  • 1
    They do actually. It's a common misnomer that search engine spiders don't execute JS. I actually run a Google Search Appliance and KNOW that it uses and executes JS on all pages it hosts. Commented May 5, 2011 at 14:15
  • Great, I didn't know that. It seems not all Javascript gets executed, and some other crawlers may not be as smart as Google (not that anyone would use these of course), so currently I would stick with a "safe" server-side replacement.
    – Jan Fabry
    Commented May 5, 2011 at 14:23
* add nofollow to links
function add_nofollow_content($content) {
    $content = preg_replace_callback(
    function($m) {
        if (strpos($m[1], "YOUR_DOMAIN_HERE") === false)
            return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
            return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
    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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.