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.
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! 🙂