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.