≡ Menu

How To Open All External Links In New Tabs (Or Windows) Without A Plugin In WordPress

porto portugal windows

Although there are both sides to the debate on whether you should or shouldn’t force all external links to open in new windows, if you decide to, don’t use a plugin to complete the job. With a few simple lines of code you can have links pointing to sites outside of your travel blog open in new tabs or windows when users click on them. Doing so potentially keeps people on your blog longer while not using a plugin helps you avoid potential conflicts with others you may be running.

Open Your functions.php File

You can find and edit your functions.php file in one of two common ways. Either through the editor in the WordPress admin interface (Appearance > Editor) or through FTP (wp-content > Themes > [Your theme in use] > functions.php). Once the file is open, copy and paste it to another file or your desktop, so you have a copy in case something goes wrong.

texas state capital buildingTwo Important Things To Note

The first is that some themes, like versions of the popular Thesis, have custom function files. You’ll want to place the code there if that’s how your particular WordPress theme is setup. (They do this to make themes more flexible with updates.)

The second is to place the code right above this – ?> – at the bottom of your functions.php file. Not doing so will either break your theme completely or result in lines of code hanging in your header. (Placing the code after the ?> or at the very top of the functions.php file are the two most common mistakes I find people make.)

Insert This Code

Be sure to replace “YOURSITE” with your particular domain name.

function autoblank($text) {
$return = str_replace(‘href=’, ‘target=”_blank” href=’, $text);
$return = str_replace(‘target=”_blank” href=”http://YOURSITE.com’, ‘href=”http://YOURSITE.com’, $return);
$return = str_replace(‘target=”_blank” href=”#’, ‘href=”#’, $return);
$return = str_replace(‘ target = “_blank”>’, ‘>’, $return);
return $return;

To have all links left by your readers in the comments section open in new windows as well, simply add these two lines to the code above:

add_filter(‘the_content’, ‘autoblank’);
add_filter(‘comment_text’, ‘autoblank’);

Hopefully now you’ve got all of the links you want opening up in new tabs or windows (which one depends on the browser settings of the viewer). In case you do notice errors, check the first two points above, and if all else fails, simply delete the code from your functions.php file.

Other Methods And Useful Lines Of Code

There are a number of alternate ways to do the same thing although I prefer this method over using jQuery. Using PHP reduces the chances of you running into code conflicts and will likely keep your travel blog loading faster as well. A few lines of code can go a long way, like using a trailing slash for site speed and improving SEO or enabling SSL for password security.

{ 4 comments… add one }
  • Sofie January 2, 2014, 15:05

    It’s very strange, but whenever I set one link in a post to open in a new tab, all the links get that setting. Both the ones that were already in the post as the ones that I add afterwards.
    Maybe it’s my theme doing it…

    • Anil Polat January 3, 2014, 06:09

      What method are you using to have the links to open in new windows?

  • Sofie January 3, 2014, 08:01

    Just the editor.
    I type the text, select the text, click the ‘add link’ button in the editor, add the link and then select the check box that says ‘open in new tab/window’. When I do that, every link in the post will get that check. It can be pretty annoying if you want some links to open in a new tab and others not.

    • Anil Polat January 4, 2014, 10:01

      It could be a theme issue or some error or improperly closed code somewhere. What if you try to modify the link specifically though HTML?

      In the editor, go to text (vs. visual), find a link and then add:


      So it looks like:

      a href=”http://link.com” target=”_blank”>

      Let me know if that results in all of the links on the page opening in new windows or just specific one.

Leave a Comment