Mar
21
2008

How To Add NoFollow To Your WordPress Post Links

By default, Wordpress adds a "nofollow" property in any anchor link in the comments section, but does not add a nofollow property in the anchor link appearing in the post content.

This hack will basically add a "nofollow" to your Wordpress post links.

Open /wp-includes/post-template.php file and find around line 53 this block of code and add the call to wp_rel_nofollow1() function on line 57 as illustrated below:

53.  function the_content($more_link_text = '(more…)', $stripteaser = 0, $more_file = ") {
54.     $content = get_the_content($more_link_text, $stripteaser, $more_file);
55.     $content = apply_filters('the_content', $content);
56.     // this is the hack:
57.     $content=wp_rel_nofollow1($content);
58.    $content = str_replace(']]>', ']]>', $content);
59.    echo $content;
60. }

Then you want to add this function to the file:

function wp_rel_nofollow1( $text ) {
global $wpdb;
$text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"',",stripslashes('$1′)) . ' rel=\"nofollow\">'", $text);
return $text;
}

Keep in mind that this is a hack of the Wordpress core file.  If you ever upgrade your Wordpress, then you will need to reapply the hack to the upgraded file.

You may see a working example site that uses this hack:

Movie Reviews (MFR)

Enjoy!

2 Responses so far

  1. Matt March 21, 2008 4:12 am

    Just add the function to your functions.php file in your theme and you wont have to edit any WP files and when you upgrade it wont be a problem :)

  2. CoderSteve (admin) March 24, 2008 2:40 pm

    Good call, Matt. That's another option, too. However, you'll need to give your the_content() function another name, and change that reference in all of your template files.

Leave a Comment

Name (required)

Email (required)

Website

Comments



You haven't activated the Flickr plugin.
If You don't have it installed
You can
Download the Flickr Plugin
from here

Recent Flickr Photo

2008 (c) InfinityScript.com - theme by ReviewSaurus