How To: Auto no-follow All Links in Posts With a Specific Tag or Category

7
0

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.

No-Follow Tag to Links

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.

Rules of Blogging - No-Follow links

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!

7 Comments

  1. Liz
    Twitter:

    June 27, 2014 at 11:13 am

    This is an interesting post because at work lately we have been going in and adding “no-follow’ to a lot of sponsored contest. Wish it was as easy as this for WordPress!
    Liz recently posted…Movie Review – Transformers: Age of ExtinctionMy Profile

    Reply

    • Shawn Ann Griffith

      June 27, 2014 at 11:19 am

      Yeah, that is one of the great things about WordPress you get to customize so much more than when using Blogger.

      Reply

  2. Dad of Divas

    June 27, 2014 at 11:49 am

    I do like wordpress because of this…but your post was great, it was well written and I learned a lot, thanks!

    Reply

  3. Regina

    June 28, 2014 at 6:26 am

    Thanks for putting this out there. I’m always leary about updating my code, but this looks like I could do it.
    Regina recently posted…Recipe: Strawberry Cream PopsiclesMy Profile

    Reply

    • Shawn Ann Griffith

      June 28, 2014 at 1:06 pm

      It is super easy to do, just copy and paste that code, change the ID number to what ID you want to have set to ‘no follow’ and then save the file. The code does all the rest for you.

      Reply

  4. Amber Louchart (Metro Detroit Mommy)

    June 28, 2014 at 8:00 am

    WordPress is great for this diversity in ability. I am still on Blogger… I’m so used to it, I can’t see moving soon. That being said, it’s a good reminder to use the no-follow tags.
    Amber Louchart (Metro Detroit Mommy) recently posted…“ScienceRiffic Comedy Show”at the Orion Township Public Library!My Profile

    Reply

    • Shawn Ann Griffith

      June 28, 2014 at 1:07 pm

      I’m actually surprised on how many bloggers are still using Blogger. It’s a nice platform, but you are limited on so much with it.

      Reply

Leave a Reply

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

CommentLuv badge