How to add nofollow to specific or all links in the_content in WordPress

To add the nofollow attribute to specific or all links in the the_content function in WordPress, you can use the wp_rel_nofollow function in your theme’s functions.php file.

Here’s an example of how you can use this function to add the nofollow attribute to all links in the the_content function:

// Add nofollow to specific or all links in the_content
// More snippets at wpunplugged.com 

add_filter( 'the_content', 'my_nofollow_content_filter' );
function my_nofollow_content_filter( $content ) {
  return wp_rel_nofollow( $content );
}

This will add the nofollow attribute to all links in the the_content function.

If you only want to add the nofollow attribute to specific links in the the_content function, you can use a regular expression to match the specific links you want to target and add the nofollow attribute to them. Here’s an example of how you can do this:

// Add nofollow to specific or all links in the_content
// More snippets at wpunplugged.com 

add_filter( 'the_content', 'my_nofollow_specific_links_filter' );
function my_nofollow_specific_links_filter( $content ) {
  $pattern = '/<a(.*?)href=[\'"](http:\/\/example\.com)[\'"](.*?)>/i';
  $replacement = '<a$1href="$2" rel="nofollow"$3>';
  $content = preg_replace( $pattern, $replacement, $content );
  return $content;
}

This will add the nofollow attribute to all links that point to the http://example.com domain. You can modify the regular expression to match the specific links you want to target.

Share this post

You might also like...

MemberPress Membership login for WordPress Review
Content restriction

MemberPress for WordPress Review

MemberPress for WordPress is a powerful membership plugin that makes it easy to create and manage memberships on any WordPress website. It allows you to

Read More »
Sucuri for WordPress Review
Plugins

Sucuri for WordPress Review

Sucuri for WordPress is a powerful security plugin that helps protect your website from hackers and malicious software. It provides a comprehensive website security solution,

Read More »

Comment

Your email address will not be published. Required fields are marked *