How to add nofollow to your wordpress posts

Posted in WordPress · September 20th, 2011 by Kazim · Comments (5)

I was searching around internet all over for adding rel=”nofollow” to external link in my posts. Suddenly, I came over a guy he has a blog that has many external links in its post but they weren’t at nofollow attribute, that blog has PR 0 and was there for more than 2 years. He randomly added no follow to his external links of WordPress posts. He raised up with PageRank 3 within very little time. Google actually links that how much you’ve been linking externally and what sites are they, So my suggestion to you is add no follow to your blog posts. Here is how.

How to add rel no follow to your WordPress posts?

  • Go to Your site > WordPress Admin Panel > Appearance > Editor (Theme editor)
  • Go to functions.php (Theme’s Function)
  • Copy the below code to the new line
function add_link_attr($func='', $args='', $attr='', $imgtag=false) {
 
    $tag = ($imgtag) ?  "<img " : "<a ";
    $tag_attr = $tag . $attr . ' ';
 
    $func_args = array();
    if(!is_array($args)) {
        $args_array = preg_split('/[\,]+/',$args);
        foreach ($args_array as $arg_array) {    
            array_push($func_args, $arg_array);
        }
    } else {
        $func_args = $args;
    }        
 
    if($func) {
        $func_args = array_map('trim', $func_args);
 
        ob_start();
        call_user_func_array($func, $func_args);
        $input = array(ob_get_contents());
        ob_end_clean();
 
        foreach($input as $line) {
            $output .= preg_replace("/$tag/", $tag_attr, $line);
        }
        echo $output;
    }
}
  • Save Changes!
  • Navigate through the page you want to enter the attribute rel nofollow. Mostly i recommend it to add it on single post page if you have summary on the home page
  • Go to single.php (for the single post view)
  • Replace the following:
  • <?php the_content(); ?>

    with

    <?php add_link_attr('the_content', 'Read more', 'rel="nofollow"'); ?>
    • Save changes

    Finally you’re done! You can now check the source of any post page that has external links.

    More Resources

    NoFollow External Links - WordPress Plugin
    Nofollowr - WordPress Plugin

    Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

    Related Posts



    5 Responses to “How to add nofollow to your wordpress posts”

    1. dona Says:

      thanks for the information, i tried this but it didn’t work!! how do i place no follow to non external links, posts links, comments, and other links?
      thank you

    2. Kazim Says:

      Dear Done,

      You want to place no follow on non external links, does this mean that you want to leave with follow to external links? Which WordPress version are you using currently?

      Thanks

    3. Peter Says:

      I’m afraid I don’t quite follow. What does “Copy the below code to the new line” mean?

      I can’t see where I put that line of code.

      Thanks
      Peter

    4. Kazim Says:

      Dear Peter,

      Put this code on new line, describing in easy words. After

      Thanks
      Kazim K.M

    5. Ferb Says:

      Really thanks, I’ve been having problem finding this and all I have to do is just to copy and paste great.

    Leave a Reply