PHP
  Home arrow PHP arrow Page 2 - Standards-compliant Link Targets with Wordpress
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Standards-compliant Link Targets with Wordpress
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-12-19


    Table of Contents:
  • Standards-compliant Link Targets with Wordpress
  • Dynamically adding rel=’no follow’
  • JavaScript link targets
  • Putting it all together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Standards-compliant Link Targets with Wordpress - Dynamically adding rel=’no follow’
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek