Open external links in a new window

Is it possible to make some sort of change or add a plugin that allows me to make every ‘external’ link open in a new window?

Just an example: if my blog was called http//timmy.com/ and I wanted to link to http//tom.com/, it would open a new window instead of the same window,
but I wanted to link from http//timmy.com/ to http//timmy.com/somewhere/ in the same window.

Read More

So I want all external link from my website to another in a new window/tab,
but I want every internal link, from my website to somewhere else on my website, to be in the same window/tab.

Is this possible?

Related posts

Leave a Reply

4 comments

  1. Yup can be done with javascript.

    var $j = jQuery.noConflict();
    
    $j(document).ready(function() { //external attribute
        $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]")
        .addClass("external")
        .attr({ target: "_blank" });
    });
    
  2. While you got answers on technical side how to do it, I want to chime in on usability side – you really shouldn’t.

    Aside from some specific cases (non-web-based documents for example) opening new browser windows/tabs for links is considered very poor practice (and for very long time already).

    While it might seems like a strategy to keep your site open in effect it is very likely to annoy users who didn’t explicitly want that new tab but got it anyway because you decided for them.

  3. Hi @ninjaboi21:

    I see @curtismchale gave you a jQuery solution which should work fine. If you’d like a PHP solution, here’s one that should work for you too.

    Basically the solution comes in two parts; first is to buffer the output using PHP’s ob_start() function, which is easy, and then to parse the output in the callback function, which is more involved. I used a not-so-elegant brute-force method; I’m sure there are better ways but the brute force method is what someone who’s not so bright like me uses. 🙂 Maybe someone else will come along with something more elegant.

    Here’s the code; put this in your theme’s functions.php file and you should be good to go:

    add_action('init','buffer_output_for_links');
    function buffer_output_for_links() {
      if (!is_admin())  // No need to do in the admin
        ob_start('decorate_links');
    }
    function decorate_links($content) {
      $wpurl = get_bloginfo('wpurl');
      $parts = explode('<a',$content);
      for($index = 1; $index<count($parts); $index++) {
        // Break apart on '>' to isolate anchor attributes
        $part = explode('>',$parts[$index]);
        // Remove all target="_blank"
        $part[0] = preg_replace("#\s*target\s*=\s*["'].*?['"]\s*#",' ',$part[0]);
        // Add target="_blank" to all
        $part[0] = preg_replace('#hrefs*=s*#','target="_blank" href=',$part[0]);
        // Remove target="_blank" from only this domain
        $part[0] = preg_replace("#target="_blank" href=(['"]){$wpurl}#","href=\1{$wpurl}",$part[0]);
        // End this part by reassembling on the '>'
        $parts[$index] = implode('>',$part);
      }
      // Finally reassembling it all on the '<a' leaving a space for good measure
      $content = implode('<a ',$parts);
      return $content;
    }
    

    -Mike Schinkel