Adding Nofollow Attributes to WordPress Comment Links
In WordPress, comment links are standard hyperlinks by default. For SEO or to control link equity, you may want to add a rel="nofollow" attribute to these links. This article explains a standard and effective method.
The Limitation of Common Approaches
Many developers first consider using the comments_popup_link() function and try to modify its output via filters. However, this function does not directly provide a filter for altering link attributes. This is by design, not a bug.
The Correct Implementation Method
Before outputting the comment popup link's <a> tag, WordPress executes this code:
echo apply_filters( 'comments_popup_link_attributes', '' );
This means we can use the comments_popup_link_attributes filter hook to inject additional HTML attributes into the link.
Step-by-Step Instructions
- Edit Your Theme File: Open the
functions.phpfile in your current WordPress theme directory. - Add the Code: Before the closing
?>tag (if present), add this code snippet:function add_nofollow_to_comments_popup_link() { return ' rel="nofollow" '; } add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link'); - Save and Activate: Save the
functions.phpfile. After refreshing your site, all comment links generated bycomments_popup_link()will automatically include therel="nofollow"attribute.
Important Notes and Best Practices
- Version Compatibility: This method works with mainstream WordPress versions. Ensure your theme or plugins don't override this core hook.
- Code Safety: Before modifying
functions.php, create a child theme or a full backup to prevent losing changes during theme updates. - Scope: This method only affects links generated by
comments_popup_link(). For links created manually or by specific plugins, you'll need a different approach.
Following these steps allows you to safely add the nofollow attribute to WordPress comment links for your SEO strategy.