X

Add nofollow to Specific or All Links in the_content

This snippet will find anchors pointing to a specific URL and add rel="nofollow". This is just a simple snippet you can use to update content across your entire site. Be sure to update the URL to something that’ll work for you. Keep in mind, you can also change rel="nofollow" to any number of other attributes -- e.g.: a custom class, or a target like _blank. Just add this snippet to the functions.php of your WordPress theme.

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

The following snippet will add rel="nofollow" to all links within the_content().

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

Comments  Leave a Reply

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!