PHP
  Home arrow PHP arrow Page 2 - Standards-compliant Link Targets with ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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? 
PHP

Standards-compliant Link Targets with Wordpress
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    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

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway