17

How To Remove nofollow From Your Comments

Comment links on New2WP DOfollow. Here's a how you can remove nofollow from links in your comments too. Give some incentive for visitors to comment on your articles, for the SEO benefits this provides....
How To Remove nofollow From Your Comments

What the Hook? - Adding the Filters

What's the hook gonna be? Well, that's what I'm showing you here. These filters are used to remove nofollow from links in the comments section.

/**
 * Removes the value ›nofollow‹ from the rel attribute.
 *
 * @author Thomas Scholz <http://toscho.de>
 * @contributor David Naber <http://blog.dnaber.de/>
 * @version 1.1
 * @license GPL 3 <http://www.gnu.org/licenses/gpl.html>
 * @param string $str Zu filternder String
 * @return string
 */
remove_filter( 'pre_comment_content', 'wp_rel_nofollow' );
add_filter( 'get_comment_author_link', 'xwp_dofollow' );
add_filter( 'post_comments_link',      'xwp_dofollow' );
add_filter( 'comment_reply_link',      'xwp_dofollow' );
add_filter( 'comment_text',            'xwp_dofollow' );

Of course, the filters wouldn't be much use without the function they are calling. Here is that function, and the funky code which it will run.

// remove nofollow from comments
  function xwp_dofollow($str) {
	  $str = preg_replace(
	  '~<a ([^>]*)\s*(["|\']{1}\w*)\s*nofollow([^>]*)>~U',
	  '<a ${1}${2}${3}>', $str);
	  return str_replace(array(' rel=""', " rel=''"), '', $str);
  }

This function uses a regular expression (regex) which it checks for any <a> element that has a rel attribute with a value of "nofollow", and replaces it then returns the new value.

All you need to do is drop this in your functions.php file to make your site dofollow. If you have any questions let me know below in the comments.

Source: http://toscho.de/2009/no-no-no-nofollow/

Shortlink:

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!

About the Author

Jared is from Boston working as a web and graphic designer. Also owns the design blog Tweeaks.com, and has designed many other websites powered by Wordpress including the New2WP theme.

Level: Noob

User Comments

( ADD YOURS )

  1. The only place you should no-follow is in to comments? or place it is also important to links external no-follow?


  2. Anyone can confirm if this indeed works on WordPress 3.0? All the nofollow removers I tried so far doesn't do anything :S.


  3. Yes it does work just fine. I use it here on this site. View any of the comments from people that have entered their website url when commenting, or any comments which have a link in the comment body. Neither are nofollow links, and this site is running 3.0.


  4. When I put those codes in my theme's fonction.php, I get the dreaded blank screen of death with some PHP error on it. I guess I'm doing it wrong.


  5. I just realized that the syntax highlighter is breaking the code. here's what it should be showing.
    Try this, it's the correct code.

    // remove nofollow from comments
      function xwp_dofollow($str) {
    	  $str = preg_replace(
    	  '~<a ([^>]*)\s*(["|\']{1}\w*)\s*nofollow([^>]*)>~U',
    	  '<a ${1}${2}${3}>', $str);
    	  return str_replace(array(' rel=""', ""), '', $str);
      }
    

    But, this is what it's showing though, which is incorrect.

     // remove nofollow from comments
      function xwp_dofollow($str) {
    	  $str = preg_replace(
    	  '~&amp;amp;lt;a ([^&amp;amp;gt;]*)\s*([&amp;quot;|\']{1}\w*)\s*nofollow([^&amp;amp;gt;]*)&amp;amp;gt;~U',
    	  '&amp;amp;lt;a ${1}${2}${3}&amp;amp;gt;', $str);
    	  return str_replace(array(' rel=&amp;quot;&amp;quot;', &amp;quot;&amp;quot;), '', $str);
      }
    


  6. Thanks for the quick reply, I changed the code as shown and the blank screen is gone. Hoeever, the rel="nofollow" tag is still there after comments(I tried posting some new comments and it is still there.) I'm guessing it might be my theme(Mystique) that's interfering with the filter. Thanks!


  7. It may be your theme. I'm not familiar with that theme so it's possible.

    Did you add the action hooks too? You need to add this entire code to the functions.php. If this is all there, and it's not working then it must be something beyond my control, like the theme, or another error somewhere in your theme files, or a plugin possibly.

    
    // remove nofollow from comments
    remove_filter( 'pre_comment_content', 'wp_rel_nofollow' );
    add_filter( 'get_comment_author_link', 'xwp_dofollow' );
    add_filter( 'post_comments_link',      'xwp_dofollow' );
    add_filter( 'comment_reply_link',      'xwp_dofollow' );
    add_filter( 'comment_text',            'xwp_dofollow' );
    function xwp_dofollow($str) {
    	$str = preg_replace( '~<a ([^>]*)\s*(["|\']{1}\w*)\s*nofollow([^>]*)>~U',
    	'<a ${1}${2}${3}>', $str);
    	return str_replace(array(' rel=""', ""), '', $str);
    }
    


  8. realy it is very good logic for nofollow & dofollow


  9. thanks mate!
    pushpinder bagga recently posted..A Construction Site Abstract


  10. Thanks for the code.


  11. Icechen,
    I use Mystique as well and had the nofollow off until the last upgrade.
    The gentleman that built that theme is good about helping.
    I will have to get with him or did you figure it out?

    Thanks


  12. I figured out how to change the nofollow to follow on the Mystique theme.

    You need to edit two locations in the core.php file.

    Just do a 'find' in the core.php file and you will find two lines with the nofollow tag.

    Change that and all the comments on your site will switch to dofollow.

    Cheers


  13. Excellent article, thanks for the code, I shall try to make it work !

  1. Avatar

    Your Name
    May 17


    CommentLuv badge