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.



