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

by Anil Polat · 5 comments

in SEO,Technical

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.

{ 1 trackback }

The Best 15 Plugins For Travel Blogs (Updated: Feb. 2013) - Travel Blog Advice
February 2, 2014 at 14:00

{ 4 comments… read them below or add one }

Leave a Comment

Previous post:

Next post: