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?

  • @Mike: Why does this question have such a weird migration history? WPSE -> Meta -> WPSE?
    – Jan Fabry
    Jan 1, 2011 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. Jan 1, 2011 at 20:38

4 Answers 4


Yup can be done with javascript.

var $j = jQuery.noConflict();

$j(document).ready(function() { //external attribute
    .attr({ target: "_blank" });
  • @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? Dec 31, 2010 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, 2011 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." Jan 1, 2011 at 20:40

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.

  • 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. Jan 1, 2011 at 13:23
  • 2
    Bah! I totally disagree. I hate it when external links don't open in a new tab. Jan 9, 2011 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, 2011 at 21:25
  • 1
    I still disagree. Jan 9, 2011 at 21:41

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


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:

function buffer_output_for_links() {
  if (!is_admin())  // No need to do in the admin
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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