How to Add Nofollow to All Outbound Links on Your WordPress Site?

In the world of SEO, “link juice” is a valuable commodity. When you link to another website, you pass some of your SEO value to them. This can be a great way to support other sites you admire, but it’s also important to manage where that link juice flows. Nofollow links tell search engines not to pass on your SEO value, and for some outbound links, this might be the best strategy.

This guide will show you how to customize all outbound links on your WordPress website to include the nofollow attribute using code snippets.

Why Use Nofollow Links?

There are several reasons you might want to use nofollow links:

  • Sponsored Content: If you’re linking to sponsored content or affiliate links, using nofollow prevents you from unintentionally passing on SEO value.
  • Untrusted Websites: Linking to websites with questionable content or practices can reflect poorly on your own site. Nofollow helps prevent this association.
  • Comments and Forums: You don’t necessarily want to endorse every link posted in comments or forum sections. Nofollow helps manage this.

Adding Nofollow to All Outbound Links with Code

While WordPress doesn’t offer a built-in option to add nofollow to all outbound links, you can achieve this using code snippets added to your theme’s functions.php file. Here’s what you need to do:

1. Accessing Your Theme’s functions.php File:

  • Login to your WordPress dashboard.
  • Navigate to Appearance > Theme Editor.
  • Select the theme you want to modify (usually the active theme).
  • On the right-hand side, locate the functions.php file and click on it to open it in the editor.

2. Adding the Code Snippets:

Carefully copy and paste the following code snippets into your theme’s functions.php file:

add_filter( 'the_content', 'add_nofollow_to_external_links' );
add_filter( 'the_excerpt', 'add_nofollow_to_external_links' );
add_filter( 'widget_text_content', 'add_nofollow_to_external_links' );

function add_nofollow_to_external_links($content) {    
    return preg_replace_callback('/<a[^>]+/', 'replace_link_with_nofollow_callback', $content);
}

function replace_link_with_nofollow_callback($link) {
    
    $nofollolink = $link[0];
    $home_url = home_url();

    if (strpos($nofollolink, 'rel') === false) {
        $nofollolink = preg_replace("%(href=\S(?!$home_url))%i", 'rel="nofollow" $1', $nofollolink);
    } elseif (preg_match("%href=\S(?!$home_url)%i", $nofollolink)) {
        $nofollolink = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $nofollolink);
    }
    
    return $nofollolink;

}

Use code with caution.content_copy

3. Saving Your Changes:

Once you’ve added the code snippets, carefully review your work for any typos. Then, click the “Update File” button to save your changes to the functions.php file.

Important Considerations:

  • Child Theme: It’s highly recommended to make these changes using a child theme to avoid losing them during theme updates.
  • Testing: After saving your changes, thoroughly test your website to ensure all outbound links now include the nofollow attribute. You can use browser developer tools to inspect the links and their attributes.
  • Plugin Alternatives: While this guide uses code, there are also plugins available that offer similar functionality for adding nofollow links. Consider your comfort level with code before diving in.

Conclusion

By following these steps and adding the provided code snippets, you can customize all outbound links on your WordPress website to include the nofollow attribute. Remember, this approach offers more granular control over your SEO strategy, but it requires some technical knowledge. If you’re not comfortable with code, consider using a plugin or consulting a developer for assistance.

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.