Making all external links open in a new tab or window in WordPress

Last updated on February 1, 2010. Tags: , ,

Website visitors need to know in one way or another if clicking on a link will lead to a webpage of the same site or to another website. One popular way of doing this is to open the links within the same website (internal links) in the same window and the links to another website (external link) in another window or tab.

Whether the linked site opens in a new window or a new tab depends on the browser. Hence, when I said " new window", it really means new window or new tab.

WordPress' visual editor allows the webmaster to set the hyperlink as either opening in the same window or in a new window by inserting target attribute: target="_blank" to open the linked site to a new window and target="_self" to open in the same window. The latter is useless because all browsers (at least, all major ones that many of us know) default to opening the link in the same window if no target was specified.

wordpress link interface

However, it can be time consuming to setup links to open in a new window one by one. We need something that will allow WordPress to detect all external links, distinguish them from internal links and insert target="_blank" attribute to each of them.

To have WordPress insert target="_blank" attribute to all external links in the post, insert the following codes in functions.php. Simply replace domain.com with the actual domain of your site.

function autoblank($text) {
$return = str_replace('<a href=', '<a target="_blank" href=', $text);
$return = str_replace('<a target="_blank" href="http://domain.com', '<a href="http://domain.com', $return);
$return = str_replace('<a target="_blank" href="#', '<a href="#', $return);
$return = str_replace(' target="_blank">', '>', $return);
return $return;
}
add_filter('the_content', 'autoblank');
add_filter('comment_text', 'autoblank');

This code will insert target="_blank" to all external links in your content and comments. This is how it works:

For this to work, be sure that you are consistent in using either the www or non-www version of the domain. Even if you already implemented the URL canonization using .htaccess, you still need to change all non-www to www or all www to non-www (depending on which version you choose) for this code to work. You can do this by doing search and replace on the database, either manual or with the aid of a plugin. Another solution is to insert the function canonical in the functions.php to make all internal links consistent. However, if you opt for this method, the function canonical should come first in the functions.php before the function autoblank that we discussed in this entry.

Posted by Greten on December 15, 2009 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by David Scott on 04.23.10 8:40 pm

    Thanks for the code. Provides a simple solution to the external linking target without the need for a plugin.

  2. Posted by Amy on 04.26.10 3:50 am

    Thank you so much! This is exactly what I was looking for. I dropped it into my functions.php and it worked without a hitch.

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.