[wp-hackers] XHTML Strict compliant replacement for target=_new

Geoffrey Lane geoff at zorched.net
Thu Oct 20 20:52:56 GMT 2005


Hi, I'm new to the list and new to WordPress (just got it yesterday, and 
I think it's great so far).

One issue I had with adding sidebar links was the target=_new not being 
compliant with XHTML Strict. There's an easy way to emulate that 
behavior with an onclick event:

onclick="window.open(this.href); return false;"

I hard coded that into my sidebar links for the 1.5 system I've got up, 
but thought I'd provide a better? solution for the future. Basically, 
I've added another option below the 'Target' option on the add link page 
to toggle the 'Onclick open new window' trick. I added a column to the 
links table to support the Y/N choice for this flag.

As it's my first WordPress patch, I'd love to get feedback/comments. Is 
this useful for other people? Is it implemented well (I'm no PHP expert 
by any means). Any suggestions for improvement?




--- Unified DIFF against SVN HEAD Below ---

Index: wp-admin/admin-db.php
===================================================================
--- wp-admin/admin-db.php	(revision 2957)
+++ wp-admin/admin-db.php	(working copy)
@@ -268,6 +268,9 @@

  	if ( empty($link_target) )
  		$link_target = '';	
+	
+	if ( empty($link_open_new) )
+		$link_open_new = 'N';

  	if ( empty($link_visible) )
  		$link_visible = 'Y';
@@ -279,12 +282,13 @@
  		$wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
  			link_name='$link_name', link_image='$link_image',
  			link_target='$link_target', link_category='$link_category',
+			link_open_new='$link_open_new',
  			link_visible='$link_visible', link_description='$link_description',
  			link_rating='$link_rating', link_rel='$link_rel',
  			link_notes='$link_notes', link_rss = '$link_rss'
  			WHERE link_id='$link_id'");
  	} else {
-		$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, 
link_image, link_target, link_category, link_description, link_visible, 
link_owner, link_rating, link_rel, link_notes, link_rss) 
VALUES('$link_url','$link_name', '$link_image', '$link_target', 
'$link_category', '$link_description', '$link_visible', '$link_owner', 
'$link_rating', '$link_rel', '$link_notes', '$link_rss')");
+		$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, 
link_image, link_target, link_open_new, link_category, link_description, 
link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) 
VALUES('$link_url','$link_name', '$link_image', '$link_target', 
'$link_open_new', '$link_category', '$link_description', 
'$link_visible', '$link_owner', '$link_rating', '$link_rel', 
'$link_notes', '$link_rss')");
  		$link_id = $wpdb->insert_id;
  	}
  	
Index: wp-admin/edit-link-form.php
===================================================================
--- wp-admin/edit-link-form.php	(revision 2957)
+++ wp-admin/edit-link-form.php	(working copy)
@@ -199,12 +199,12 @@
           &nbsp;<?php _e('(Leave at 0 for no rating.)') ?> </td>
           </tr>
           <tr>
-           <th scope="row"><?php _e('Target') ?></th>
+           <th scope="row"><?php _e('Target:') ?></th>
             <td><label>
-          <input type="radio" name="target" value="_blank"   <?php 
echo(($link->link_target == '_blank') ? 'checked="checked"' : ''); ?> />
+          <input type="radio" name="link_target" value="_blank"   <?php 
echo(($link->link_target == '_blank') ? 'checked="checked"' : ''); ?> />
            <code>_blank</code></label><br />
  <label>
-<input type="radio" name="target" value="_top" <?php 
echo(($link->link_target == '_top') ? 'checked="checked"' : ''); ?> />
+<input type="radio" name="link_target" value="_top" <?php 
echo(($link->link_target == '_top') ? 'checked="checked"' : ''); ?> />
  <code>_top</code></label><br />
  <label>
  <input type="radio" name="link_target" value=""     <?php 
echo(($link->link_target == '') ? 'checked="checked"' : ''); ?> />
@@ -212,11 +212,23 @@
  <?php _e('(Note that the <code>target</code> attribute is illegal in 
XHTML 1.1 and 1.0 Strict.)') ?></td>
           </tr>
           <tr>
+         	<th scope="row"><?php _e('Onclick Open New Window:') ?></th>
+         	<td>
+         	<label>
+<input type="radio" name="link_open_new" value="Y" <?php 
echo(($link->link_open_new == 'Y') ? 'checked="checked"' : ''); ?> />
+<?php _e('Yes') ?></label><br />
+         	<label>
+<input type="radio" name="link_open_new" value="N" <?php 
echo(($link->link_open_new != 'Y') ? 'checked="checked"' : ''); ?> />
+<?php _e('No') ?></label><br />
+<?php _e('(This is an XHTML 1.1 and 1.0 Strict compliant way of doing a 
popup window.)') ?>
+         	</td>
+         </tr>
+         <tr>
             <th scope="row"><?php _e('Visible:') ?></th>
             <td><label>
-             <input type="radio" name="link_visible" <?php if 
($link->link_visible == 'Y') echo "checked='checked'"; ?> value="Y" />
+             <input type="radio" name="link_visible" <?php if 
($link->link_visible != 'N') echo "checked='checked'"; ?> value="Y" />
  <?php _e('Yes') ?></label><br /><label>
-<input type="radio" name="visible" <?php if ($link->link_visible == 
'N') echo "checked='checked'"; ?> value="N" />
+<input type="radio" name="link_visible" <?php if ($link->link_visible 
== 'N') echo "checked='checked'"; ?> value="N" />
  <?php _e('No') ?></label></td>
           </tr>
  </table>
Index: wp-admin/link-add.php
===================================================================
--- wp-admin/link-add.php	(revision 2957)
+++ wp-admin/link-add.php	(working copy)
@@ -7,7 +7,7 @@


  $wpvarstoreset = array('action', 'cat_id', 'linkurl', 'name', 'image',
-                       'description', 'visible', 'target', 'category', 
'link_id',
+                       'description', 'visible', 'target', 
'link_open_new', 'category', 'link_id',
                         'submit', 'order_by', 'links_show_cat_id', 
'rating', 'rel',
                         'notes', 'linkcheck[]');
  for ($i=0; $i<count($wpvarstoreset); $i += 1) {
Index: wp-admin/link-manager.php
===================================================================
--- wp-admin/link-manager.php	(revision 2957)
+++ wp-admin/link-manager.php	(working copy)
@@ -8,7 +8,7 @@
  $this_file = $parent_file = 'link-manager.php';

  $wpvarstoreset = array('action','cat_id', 'linkurl', 'name', 'image',
-                       'description', 'visible', 'target', 'category', 
'link_id',
+                       'description', 'visible', 'target', 
'link_open_new', 'category', 'link_id',
                         'submit', 'order_by', 'links_show_cat_id', 
'rating', 'rel',
                         'notes', 'linkcheck[]');

Index: wp-admin/upgrade-schema.php
===================================================================
--- wp-admin/upgrade-schema.php	(revision 2957)
+++ wp-admin/upgrade-schema.php	(working copy)
@@ -53,6 +53,7 @@
    link_name varchar(255) NOT NULL default '',
    link_image varchar(255) NOT NULL default '',
    link_target varchar(25) NOT NULL default '',
+  link_open_new enum('Y','N') NOT NULL default 'N',
    link_category bigint(20) NOT NULL default '0',
    link_description varchar(255) NOT NULL default '',
    link_visible enum('Y','N') NOT NULL default 'Y',
Index: wp-includes/links.php
===================================================================
--- wp-includes/links.php	(revision 2957)
+++ wp-includes/links.php	(working copy)
@@ -191,7 +191,7 @@
  		$length = '';
  	}

-	$sql = "SELECT link_url, link_name, link_image, link_target, 
link_description, link_rating, link_rel $length $recently_updated_test 
$get_updated FROM $wpdb->links WHERE link_visible = 'Y' " . $category_query;
+	$sql = "SELECT link_url, link_name, link_image, link_target, 
link_open_new, link_description, link_rating, link_rel $length 
$recently_updated_test $get_updated FROM $wpdb->links WHERE link_visible 
= 'Y' " . $category_query;
  	$sql .= ' ORDER BY ' . $orderby . $direction;
  	/* The next 2 lines implement LIMIT TO processing */
  	if ($limit != -1)
@@ -239,8 +239,15 @@
  		if ('' != $target) {
  			$target = ' target="' . $target . '"';
  		}
+		
+		$newwindow = $row->link_open_new;
+		if ('Y' == $newwindow) {
+			$newwindow = ' onclick="window.open(this.href); return false;"';
+		} else {
+			$newwindow = '';
+		}

-		$output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
+		$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . 
$newwindow . '>';

  		if (($row->link_image != null) && $show_images) {
  			if (strstr($row->link_image, 'http'))


-- 
Geoff Lane <geoff at zorched.net>


More information about the wp-hackers mailing list