Add nofollow tag to WordPress posts →

Posted on August 8, 2008 in Development with 0 comments.

I must have posted about adding the nofollow tag to WordPress posts about 10 times on various blogs I have run over the years and all those blogs are no longers live, so I thought I would post it once more and just leave it up :)

Adding the nofollow tag to links in your WordPress posts is actually fairly simple. It is just a matter of adding a little php code into your themes functions.php file, saving and uploading it. Lets get started…

Step 1:

Open your functions.php file of your theme. If you don’t have one, create it and put it in your main theme directory.

Step 2:

Add the following code into the file.

<?php
// Add nofollow tag to links in your posts
function addnofollow($text) {
    return str_replace('<a href=', '<a rel="nofollow" href=',  $text);
}
add_filter('the_content', 'addnofollow');
?>

IMPORTANT! Make sure that you don’y leave any blank lines after the code. If you do, you will get a nasty php error every time you try view your blog.

Step 3:

Save and upload to your theme directory.

Every time you create links in your WordPress posts they will now have the nofollow tag added to them :) Questions, comments, errors? Leave them in the comment box below.

Tagged with ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>