Open external URLs in a new window
Personally and professionally I think links should open in the same tab, but some clients insist on opening external links in new tabs.
<?php
/**
* Override theme_menu_item_link
* http://api.drupal.org/api/function/theme_menu_item_link/5
*/
function phptemplate_menu_item_link($item, $link_item) {
// make external links open in new windows
$attributes = !empty($item['description']) ? array('title' => $item['description']) : array();
if (strpos($link_item['path'], 'http://') === 0 || strpos($link_item['path'], 'https://') === 0) {
$attributes['target'] = '_blank';
}
return l($item['title'], $link_item['path'], $attributes, isset($item['query']) ? $item['query'] : NULL);
}
?>
Another example, from a view template:
<?php
$attributes = '';
// ...
if (strpos($field_link_url, 'http://') === 0 || strpos($field_link_url, 'https://') === 0) {
$attributes .= ' target = "_blank"';
}
?>
<p>
<a href="<?php print $field_link_url ?>"<?php $attributes ?>>
<?php print $image ?>
</a>
</p>
Comments
Post new comment