X

How to Add nofollow to Specific or All Links in the_content

Snippets by IsItWP

Are you looking for a way to add nofollow to specific or all links in the_content? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add nofollow to specific or all links in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function example_link_nofollow( $content ) {
	$url     ='http://example.com';
        $content = str_replace( '<a href="'.$url.'', '<a rel="nofollow" href="'.$url.'', $content );
	return $content;
}
add_filter('the_content','example_link_nofollow');

The above code will add nofollow to specific links with the URL mentioned above. To add nofollow to all links within the_content(), you need to add the following code:

function example_all_links_nofollow( $content ){
        $content = str_replace( '<a href="', '<a rel="nofollow" href="', $content );
	return $content;
}
add_filter('the_content','example_all_links_nofollow');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and 8 best WordPress review plugins.

Comments   Leave a Reply

  1. Guilherme Euler July 14, 2019 at 11:01 pm

    Hello friend,

    First, thanks for your solution… but I have a question… your first code is used to one especifc URL, on example, “http://example.com”… but if I need to add to some URLs? “http://example1.com”, “http://example2.com” and “http://example3.com”

    Can you help me with this?

    For now, thank you very much! 🙂

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!