What we have here is another one of those how-to posts that came to be all because I wanted to do away with a plugin and put the coding directly into my theme.  The best part of this coding is that you don’t have to worry about putting the post in one particular category in order for the links within a sponsored/paid post get set to no-follow.  We all know that sometimes a post fits better in one category over another and well, you don’t always want that one category to be set to no-follow.

Wanting to automatically add the no-follow code to links within a specific category or tag? Add a small bit of code to your theme and you are all set.

This was the case for me. Because I am trying to narrow down my categories I wanted to do away with the Review category and put the posts into more appropriate categories; Home & Garden, Kids, Family, etc. Rather than a generic category of Reviews.  Well, not all the posts I do in these categories are paid posts or reviews, so using the plugin I had used was no longer relevant.

I was able to find two small pieces of code to do what a plugin used to do; only it’s better!  Now I have it set that any posts that I put into the Sponsored Post category have the no-follow tag added to the links within the post automatically.  Since the category Sponsored Posts is so generic I only use this for anything that doesn’t fall within my primary categories; primarily affiliate posts and posts through campaigns that I don’t review a product.  I use similar code to add the no-follow tag to all posts that are tagged as a review. This would be for all posts that I review a product, this way the post can go into the Family, Kids, or any other category I please.

OK, are you ready for the simple little codes?  For this, you will need to edit your functions.php file.  These bits of code can be placed at the end of your current file.

No, Follow links within a particular tag

For this code to work you will need to tag each post with the correct tag; in my case Review, in the Tags section of your post edit page.

function nofollow_tag_posts($text) {
global $post;
if( has_tag(7736) ) {
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_tag_posts');

You will need to change the tag ID 7736 to the right tag ID you want to set to no-follow.  To find out the correct tag ID, you need to click on the Tags menu (located in the Posts section). Locate the tag that you want to use and hover over the name of the tag.  In your status bar, you will see the direct link to that tag, and you will see post_tag&tag_ID=7736&post_type=post.  The tag ID is the only number in this URL.  You can alternately click on the tag and look in the address bar and see the URL with the tag ID.

No-Follow links within specific category

For this code to work you will need to check the correct category for each post; in my case Sponsored Posts, in the Categories section of your post edit page.

function nofollow_cat_posts($text) {
global $post;
if( in_category(6708) ) {
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_cat_posts');

You will need to change the category ID 6708 to the correct category ID you want to set to no-follow.  Follow the directions above for the tag ID to find out what the category ID is.

The only problem I have found with these codes is that I cannot figure out how to set it up for multiple tags and/or categories.  I will figure it out one day!