Add Nofollow Attribute to All Links in a Specific Category

How to Add Nofollow Attribute to All Links in a Specific Category

in Categories
Google+ LinkedIn

Adding the html attribute rel=”nofollow” to a link effectively stops Google and some other search engines to follow the link and prevent our website from passing page rank or recommendation to them. So adding the nofollow attribute to certain hyperlinks is good for SEO. Specially affiliate links, marketing link, paid links, links within blogrolls and comments, links crediting images, etc., are all good examples of outbound external links that should be nofollowed. Using nofollow for such links, Google won’t follow them, and you’ll remain in good standing with the search engine.

It’s easy to add nofollow attribute to a links, you just need to add rel=”nofollow” to it. But if majority of posts in a specific category contains mostly affiliate or marketing links then it would be good option to automatically add nofollow attribute to all links in posts from that category, instead of adding nofollow to each link.

Simply paste this code snippet in your theme’s functions.php file to add nofollow attribute to all links in a category. Don’t forget to change the desired category ID in following WordPress code snippet. You can specify category by ID (integer), name or slug (string), or an array of these.

// add nofollow attribute to all links in a specific category
function wcs_all_nofollow_category_posts( $text ) {
    global $post;
    if( in_category(1) ) {
        $text = stripslashes( wp_rel_nofollow($text) );
    }
    return $text;
}
add_filter( 'the_content', 'wcs_all_nofollow_category_posts' );

Share the love

If you like this snippet, share it with others!

Leave a Reply

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