Home arrow PHP arrow Page 2 - Standards-compliant Link Targets with Wordpress

Dynamically adding rel - PHP

Wordpress is an extremely popular site blogging software written in PHP. It does a great job of creating standards-compliant web sites. Plus it’s a good idea to continue with that effort when adding modifications.

  1. Standards-compliant Link Targets with Wordpress
  2. Dynamically adding rel=’no follow’
  3. JavaScript link targets
  4. Putting it all together
By: Nilpo
Rating: starstarstarstarstar / 4
December 19, 2007

print this article



The solution you are going to see is in the form of a Wordpress plugin.  If you are not using a Wordpress site, this can certainly be modified to work for you too.  This could easily be a Joomla mambot, for example.  The key is to make sure that this code is executed before your page is displayed to the end-user.

It’s also important to understand that this needs to be accomplished server-side.  You could easily write a JavaScript to perform this task when a page loads; however, that would be ineffective for two reasons: primarily because search engine robots don’t use JavaScript, and because JavaScript is executed after the page load completes.

While I’ve chosen PHP because Wordpress is written in PHP, you could easily use ASP or any other server-side solution to accomplish this task.

When I decided to make this plugin for Wordpress, I knew of a similar solution (Wikipedia nofollow by Ken Yasumoto-Nicolson) that added rel=”nofollow” to Wikipedia links.  Since it is released under the GNU GPL, I decided to save some time and just edit this code to work for me.

The code actually works pretty easily.  When a page is created, it checks for any links.  It then processes each link, in turn, to see if it is internal or external and then adds the rel=”nofollow” attribute as needed.  It even checks to make sure that the attribute doesn’t exist already.

To use this plugin, just drop the external_links.php file into your plugin folder and activate it.  You’re all set.

However, if you’re like me, you have multiple sites.  I don’t want to exclude my own sites.  But since the domain is different, they also get the rel attribute.  Here’s how you can change it so that doesn’t happen.

Open external_links.php in any text editor and find the section below in the parse_nofollow() function.

if ( wp_get_domain_name_from_uri($matches[3]) != $local_domain &&

   wp_has_no_rel_nofollow( $matches[1] ) &&

   wp_has_no_rel_nofollow( $matches[4] ) )


This is the section of code that determines what links will have the rel attribute added to them.  It requires that they do not link to the local domain and that they do not already have a rel=”nofollow” attribute.  We can add more domains to exclude by adding a line like the following:

wp_get_domain_name_from_uri($matches[3]) != "myotherdomain.com" &&

This line simply excludes anything that links to the domain provided.  You can add as many as needed for all the domains that you own.  Your final code should look something like this:

if ( wp_get_domain_name_from_uri($matches[3]) != $local_domain &&

   wp_get_domain_name_from_uri($matches[3]) != "myotherdomain.com" &&

   wp_get_domain_name_from_uri($matches[3]) != "anotherdomain.com" &&

   wp_has_no_rel_nofollow( $matches[1] ) &&

   wp_has_no_rel_nofollow( $matches[4] ) )


Now every external link on your site will have rel=nofollow dynamically added to the "a" tag.

>>> More PHP Articles          >>> More By Nilpo

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: