How to set nofollow rel attribute to all outbound links in WordPress? Any Plugin?

I want to know how to automatically set all links to nofollow in WordPress. Is there a WP Plugin that will make all my outbound links nofollow?

Help is greatly appreciated!

Related posts

Leave a Reply

3 comments

  1. If you are make this change for SEO optimization, you can’t make the change only with JS because the google bots don’t read the content generated by Javascript.

    But, you can add a filter in your function.php like this:

    function rel_nofollow( $content ) {
        return preg_replace_callback( '/<a[^>]+/', 'rel_nofollow_callback', $content );
    }     
    add_filter( 'the_content', 'rel_nofollow', 99999 );
    
    function rel_nofollow_callback( $matches ) {
        $link = $matches[0];
        $exclude = '('. home_url() .'|http://([^.]+.)?(wp.org|wp.com))';
        if ( preg_match( '#href=S('. $exclude .')#i', $link ) )
            return $link;
    
        if ( strpos( $link, 'rel=' ) === false ) {
            $link = preg_replace( '/(?<=<as)/', 'rel="nofollow" ', $link );
        } elseif ( preg_match( '#rel=S(?!nofollow)#i', $link ) ) {
            $link = preg_replace( '#(?<=rel=.)#', 'nofollow ', $link );
        }
    
    return $link;   
    }
    

    This function set all links into the post to the attribute: rel=”nofollow”, but, if you want change all the site maybe you must try with this plugin

  2. A number of years later, the details changed but the question remains.
    For one thing, the number of relevant rel attributes has grown:

    Disregard numerous examples in older posts of code snippets to tune rel= attributes, as simply hacking WordPress PHP is quite undesirable these days. With frequent and (optionally) automated core software and plugin updates, and with security plugins checking all the code for non-standard mods, let’s focus on the UI and plugin configuration methods instead.

    While the Classic editor gives an option to Open link in a new tab, current version of the Gutenberg editor in combination with Yoast also provides UI to select nofollow or sponsored.

    For more controls there are plugins. The one mentioned in earlier answers is still around, named External Links and with 80K installs and an exclusive focus on the link attributes, plus link icons and WPMU.
    A much more popular plugin, with 2M+ installs is All in One SEO (AIOSEO), which among many other features provides access to noindex and nofollow.
    Perhaps the most popular among SEO plugins, with 5M+ installs is Yoast SEO, but current version provides no additional help with bulk nofollow, though it adds noopener, as documented, but apparently also noreferrer to external links automatically.

    Discussion so far related to external links, as nofollow or noopener are not used for internal links. Instead, concerns about duplicate content or low quality (thin content) pages raise questions about which parts of the site navigation and interlinking to index. Yoast shines here, with fine grain support for indexing author, tag or category links and pages.

    Beyond search engine interpretation of individual links, rel=canonical tag allows merging together treatment of multiple internal links that actually lead to the same page or post. We are only guessing how Google interprets our ‘hints’ about links and hope that Yoast and WordPress produce a structure that is digestible.

    Whether a particular subset of the rel and SEO plugins mentioned, in combination with a particular version of your favorite editor can work reliably alongside each other, and how to sequence their combined workflow is entirely TBD. A safe and consistent way of iteratively making specific adjustments to subsets of external and groupings of internal links, with a special treatment of affiliate and associate links is TBD. How to tackle a substantial site already in production, and retroactively apply select auto-magical adjustments is TBD. Any detailed suggestions, links to specific information and best of all, actual experiences would be rather welcome.

    How browsers and spiders actually traverse a WordPress site is controlled at a different system level. The popular plugin for HTTP redirection is conveniently called Redirection. URL rewriting in Apache web server is configured via .htaccess file. cPanel and several system plugins, e.g. Really Simple SSL or WordFence, have automation in their UI for some of the settings, though conflicts may need to be resolved manually. These subjects are best covered in depth elsewhere.

    For coding affectionados, two earlier attempts at different approaches:
    Much tighter jQuery implementation, compared with the one below.
    Really old direct hack of the entire page HTML, fetched from DOM.

  3. Within WordPress you can select to open a link in a new window. When this is selected WordPress adds an attribute target=”_blank”. This is what I usually do with outbound links. If so you can use jQuery to add the attribute rel=”nofollow” like this

    <script type="text/javascript">
    $(document).ready(function() {
       $('a[target="_blank"]').attr('rel', 'nofollow');
    });
    </script>