How to Add Nofollow to Links in a WordPress Post

I am not talking about the simple how-to add nofollow to just a link, but rather, adding nofollow to ALL your links. There is a lot of misconception when it comes to adding the tag rel=”nofollow” within links in a WordPress post. I know, because I have had a lot of headache and have done a lot of trial and error before finding a solution to adding the rel=”nofollow tag to links that I specifically wanted to have the tag.

When, I was looking for a simple plugin that could add nofollow to links within a WordPress post, I was astonished that no one came up with such a plugin so, after some long trial and error, I have come to a simple solution that I would like to share with my readers. All you have to do is just place this code within your WordPress’s ‘Theme Functions’ file to automatically add the rel=”nofollow” tag to ALL your links within a WordPress post, but to also be able to assign dofollow to links of your wanting as well.

The Code (Sorry, my style.css doesn’t have that cool box block-quote)

<?php

Advertisements

// Add nofollow tag to links in your posts

function addnofollow($text) { $return = str_replace(‘<a href=’, ‘<a rel=”nofollow” href=’, $text); $return = str_replace(‘<a rel=”nofollow” href=”http://www.(YourDomainName).com’, ‘<a href=”http://www.(YourDomainName).com’, $return); return $return; } add_filter(‘the_content’, ‘addnofollow’);

?>

After you have simply copy and pasted this code at the bottom of your theme function file (Function.php) as well as filled in your site’s domain name, you can just hit save. Now, let me illustrate what this code means, and how you assign dofollow links within a post.

The strip of code:

function addnofollow($text) { $return = str_replace('<a href=', '<a rel="nofollow" href=', $text);

  • Is simply automatically setting all your links that use the standard a href=”URL” model to now have a rel=”nofollow” tag. So, by default, all your links will be nofollow, unless of course you want to assign the link to be dofollow. That brings us to the next step.

Since the code specifically adds rel=”nofollow” to links that start out with a href=”URL,” to bypass this, we can simply add a simple code like <a target=”_self” href=”URL”>(Anchor Keyword)</a> when you are writing your posts in the HTML view of your post write up. Some more targets you can use are…

Value Description
_blank Open the linked document in a new window
_self Open the linked document in the same frame as it was clicked (this is default)
_parent Open the linked document in the parent frameset
_top Open the linked document in the full body of the window
framename Open the linked document in a named frame

Credited to W3Schools.

Edit**

We have found a better alternative knowing that the current version of WordPress simply moves the target to the end of the link, which undermines the whole purpose of adding the target code in the first place. So in replacement of the target code, we recommend using <a title=”" href=”URL”>(Anchor Text)</a>. We hope you guys enjoy.

Now to explain why you want to add your domain name in this code:

$return = str_replace(’<a rel=”nofollow” href=”http://www.(YourDomainName).com’, ‘<a href=”http://www.(YourDomainName).com’, $return); return $return;

  • Simply what this code is doing is that whenever you address, hyperlink the specific line, it will automatically make it dofollow. The reason why I say add your domain name is because you wouldn’t want to add nofollow to your own links right? Internal links should always be dofollow.

I hope I was pretty straight forward in this tutorial and  have helped a bit of webmasters. If you have any questions or comments use the comment box below. If you find that something is not working correctly, simply report it here.

    This article was written by Albert        

Albert Fang is an Internet Marketer, a Blogger, and is someone who likes to undercover new ways to utilize the web for means of generating consistent income.

Related Posts

  • http://studyinfo.22web.net blues

    Hi
    That’s life saving article for me. I wish to ask you whether i can do it by setting custom field value in edit page/post if i don’t want to go for change in my theme code. Thanx lot.

  • http://www.mooladays.com Albert

    The code you added to your ‘Theme Functions’ is responsible for adding the rel=”nofollow” by default. Unless you manually want all links to be Dofollow by default, then you should just keep your template the way it is. If you are trying to ask if this effects pages and adds nofollow to the links in them, then the answer is yes.

    I hope this answered your question.

  • http://www.warriorforum.com/adsense-ppc-seo-discussion-forum/113013-8-reasons-why-your-blog-isn-t-seeing-search-engine-results.html#post1082434

    [...] interested in really tuning up how you use dofollow and nofollow. For WordPress users of course. How to Add Nofollow to Links in a WordPress Post ? Moola Days Hope you guys [...]

  • http://www.techvorm.com Paritosh

    What if i want to add no follow attribute only to some blog post and not to All ? Is there a plugin for doing that ?

  • Ashik

    Hi,

    I am getting an error which says “unexpected T_STRING” on the following line of your code:

    function addnofollow($text) { $return = str_replace(‘<a href=’, ‘<a rel=”nofollow” href=’, $text);

    can you help me out?

    regards,
    Ashik.

  • http://www.mooladays.com Albert

    Did you position the code in your functions.php file? And where? If your functions.php file already has the starting < ?php, you can just scroll to the bottom of your function.php file and find the ending ?> and paste the code above it.

    If you have tried this and the problem still persists, feel me to show me a small snippet of your functions.php coding so I can take a look at it.

  • http://www.cgprimorje.com AcaZ

    Is this code working for the newest WP version?

  • http://massfacebookfans.com/ increase facebook fans

    provide the latest code …its not working now 

  • http://www.electroserv.com.au Energy Management

    Of course, what a fantastic site and enlightening posts, I will bookmark your site.Best Regards!

  • http://creative-web-designer.com/ Wordpress web designer

    This article has a lot of unique and quality information. I found this to be not only well written, but engrossing and intelligent.

  • http://www.electroserv.com.au Power Factor Correction

    This is actually great content. Many thanks with this. I’ve read several excellent stuff here. Definitely value bookmarking for revisiting.thanks

  • http://massfacebookfans.com/ buy facebook fans

    Thank you for this post. I love it. Ill bookmark for sure! thanks

  • http://marathontelecom.net/m-to-m-autmobiles-sector/m2m-communications.aspx m2m communications

    This is my Good luck that I found your post which is according to my search and topic, I think you are a great blogger, thanks for helping me out from my problem..
     

blog comments powered by Disqus