Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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?

share|improve this question
    
@Mike: Why does this question have such a weird migration history? WPSE -> Meta -> WPSE? –  Jan Fabry Jan 1 '11 at 15:08
    
@Jan Fabry - Because I screwed up, and it would not let me undo. His original first paragraph made it sounds like a question for meta and I migrated too soon. Then I realized it was really about WordPress and not about StackExchange. –  MikeSchinkel Jan 1 '11 at 20:38
add comment

migrated from meta.wordpress.stackexchange.com Dec 31 '10 at 18:31

This question came from our discussion, support, and feature requests site for WordPress developers and administrators.

4 Answers

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" });
});
share|improve this answer
    
@curtishale - +1 - nice solution. I always think of using PHP unless I can't do and then I think of jQuery. Yours will work better for most use cases except for when the HTML output by the server must be as-is such as cases where jQuery won't run, which are rare. One thing: yours doesn't yet take into consideration the case where an internal link already has target="_blank" and thus want to remove it. Maybe you could update for that? –  MikeSchinkel Dec 31 '10 at 19:38
    
@Mike: If an internal link has target="_blank" set, I would assume this was done on purpose in the editor, and not remove that. –  Jan Fabry Jan 1 '11 at 15:03
    
@Jan Fabry - Yes, but that's not what the OP asked for. He explicitly said "but I want every internal link, from my website to somewhere else on my website, to be in the same window/tab." –  MikeSchinkel Jan 1 '11 at 20:40
add comment

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.

share|improve this answer
1  
Though I gave a technical answer I entirely agree. I discourage any client that wants this from doing it in the strongest terms possible. –  curtismchale Jan 1 '11 at 13:23
    
Bah! I totally disagree. I hate it when external links don't open in a new tab. –  Nick Jan 9 '11 at 21:12
    
@Nick on normal link you have a choice to open is in same tab or new one (by browser controls). Forcing link to open in new tab is taking away that choice. It is better when everyone has a choice than when some are content by making others discontent. –  Rarst Jan 9 '11 at 21:25
    
I still disagree. –  Nick Jan 9 '11 at 21:41
add comment

Well, no need to reinvent the wheel: External Links Plugin For WordPress, I can really recommend it.

share|improve this answer
add comment

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('#href\s*=\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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.