Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
    
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 May 5 '11 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 May 5 '11 at 14:31
    
Be aware that regex and HTML are a tricky combination. –  Geert May 5 '11 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 May 5 '11 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() {
    $("a[href^=http://]").attr("rel","nofollow");
});
share|improve this answer
    
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 May 5 '11 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. –  Chadster_BAM May 5 '11 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 May 5 '11 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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