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....
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!
Level: Noob






User Comments
( ADD YOURS )Power Generators June 18
The only place you should no-follow is in to comments? or place it is also important to links external no-follow?
Icechen1 July 24
Anyone can confirm if this indeed works on WordPress 3.0? All the nofollow removers I tried so far doesn't do anything :S.
Jared July 24
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.
Icechen1 July 24
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.
Jared July 24
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;lt;a ([^&amp;gt;]*)\s*([&quot;|\']{1}\w*)\s*nofollow([^&amp;gt;]*)&amp;gt;~U', '&amp;lt;a ${1}${2}${3}&amp;gt;', $str); return str_replace(array(' rel=&quot;&quot;', &quot;&quot;), '', $str); }Icechen1 July 24
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!
Jared July 24
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); }Stag Nights August 14
realy it is very good logic for nofollow & dofollow
pushpinder bagga August 30
thanks mate!
pushpinder bagga recently posted..A Construction Site Abstract
Bill Wynne October 15
Thanks for the code.
Bill Wynne October 15
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
Bill Wynne October 15
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
Electronic Business November 20
Excellent article, thanks for the code, I shall try to make it work !
Trackbacks