Add a rel=”nofollow” Attribute to All External Links on Your WordPress Blog

WordPress doesn’t automatically add a rel=”nofollow” attribute to all the external links, but if you want to add a rel=”nofollow” attribute link to all external links, you can do it by copy and paste the following coding to the functions.php file.

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;
}

Thanks to One Extra Pixel for the great code!

10 Responses to “Add a rel=”nofollow” Attribute to All External Links on Your WordPress Blog”

  1. Georgetta says:

    Hi there just wanted to give you a quick heads up. The text in your post seem to be running off the
    screen in Internet explorer. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d
    post to let you know. The design and style look great though!
    Hope you get the issue resolved soon. Kudos

  2. www.Thecupofgold.com says:

    Great site you have here.. It’s difficult to find high quality writing like yours these days. I honestly appreciate people like you! Take care!!

  3. Antonio says:

    I like the helpful information you provide in your articles. I’ll bookmark your weblog and check again here frequently. I’m quite certain I will learn many new stuff right here! Best of luck for the next!

  4. Donnie says:

    Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site? My website is in the exact same niche as yours and my visitors would genuinely benefit from a lot of the information you provide here. Please let me know if this ok with you. Thanks a lot!

  5. rongrong says:

    Wow this is really great, I have to add to my website nofollow,good

  6. Eric says:

    Donnie, you can do so..it is ok as long as it is credited. Thanks!

  7. Tawanna says:

    I’m really impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays.

  8. Maple says:

    I wanted to thank you for this great read!! I certainly loved every bit of it. I’ve got you saved as a favorite to look at new things you post…

  9. Sandra says:

    It’s going to be finish of mine day, but before end I am reading this great article to improve my knowledge.

  10. Eric says:

    theme is customized by myself

Leave a Reply