Make WordPress Open External Links In A New Window Or Tab

Norman BalberanStarred Page By Norman Balberan, 3rd Oct 2013 | Follow this author | RSS Feed
Posted in Wikinut>Guides>Web>Blogs & Blogging

Any webmaster or blogger do not want visitors leave their site when they click an external link, and one good way to achieve this is to make all external links open in a new window or tab. This guide is for Wordpress users and I hope this will help you a lot.


I recently created a page with a lot of links leading to an external site and I forgot to add target=”_blank” to it so it would open in a new tab or window. It was so troublesome to edit each one of all those links so I opt and search for an easier way to make wordpress open external links in a new window or tab automatically.

How To Do It?

First, I found out about WP External Links, a plugin which totally does what I needed, but I really don’t want to make my site bloated with plugins and I just install plugins which I haven’t found any custom workarounds for my WordPress sites. So I searched for more and found this snippet.

function wp_change_target($content){
return preg_replace_callback('/<a[^>]+/', 'wp_target_callback', $content);

function wp_target_callback($matches){
$link = $matches[0];
$mu_url = get_bloginfo('url');

if (strpos($link, 'target') === false){
$link = preg_replace("%(href=\S(?!$mu_url))%i", 'target="_blank" $1', $link);
}elseif (preg_match("%href=\S(?!$mu_url)%i", $link)){
$link = preg_replace('/target=S(?!_blank)\S*/i', 'target="_blank"', $link);
return $link;
add_filter('the_content', 'wp_change_target');

All you have to do is just paste it on your functions.php file located at wp-content/themes/YOUR_TEMPLATE_FOLDER/ and save it or just go to your WordPress Dashboard – Appearance – Editor and on the right side click on functions.php.


Wordpress, Wordpress Blog, Wordpress Blogs, Wordpress Cms, Wordpress Development, Wordpress Optimization, Wordpress Plug-Ins, Wordpress Web Development, Wordpress Website, Wordpress Website Development

Meet the author

author avatar Norman Balberan
I am a Pharmacist and currently trying my luck as a writer. I currently write for some sites including my own personal site.

Share this page

moderator Mark Gordon Brown moderated this page.
If you have any complaints about this content, please let us know


author avatar Mike Robbers
3rd Oct 2013 (#)

Useful tip. Thanks!

Reply to this comment

author avatar Norman Balberan
4th Oct 2013 (#)

You are welcome Mike! If you any questions about this article and regarding wordpress, I'll be glad to help you.

Reply to this comment

author avatar cnwriter..carolina
5th Oct 2013 (#)

very impressive although have not the slightest idea what it is all about!!! will stick to my poetry but many thanks Norman..

Reply to this comment

author avatar Norman Balberan
5th Oct 2013 (#)

This guide is applicable to bloggers and webmasters who have their own self hosted wordpress sites. If you have one, then this is a good method to retain your first time visitors.

Reply to this comment

author avatar Fern Mc Costigan
14th Oct 2013 (#)

Interesting post!

Reply to this comment

author avatar Mk
12th Nov 2013 (#)

Gucci Outlet Online,

Coach Factory Outlet,

Ralph Lauren Outlet Online,

North Face Jackets Clearance,

MCM Bags Outlet Online,

Michael Kors Outlet Online,

Coach Outlet,

Coach Purse Outlet Online,

Louis Vuitton Outlet,

Michael Kors Outlet Online,

Monster Beats Outlet,

Coach Factory Store,

North Jackets Outlet Online,

Coach Factory,

Coach Purses Outlet,

Polo Outlet,

Louis Vuitton Outlet Online,

Ugg Bailey Button Factory,

Coach Outlet Online,

Michael Kors Outlet,

Reply to this comment

author avatar Django
28th Dec 2013 (#)

Celtics president of basketball operations Danny Ainge and Green's agent <a href="">Coach Outlet Online</a> David Falk, maintained during the lengthy delay that the deal eventually would get done <h1><a href="">Coach Factory Online</a></h1> but wouldn't shed light on what was holding up the process <h1><a href=""title="Coach Outlet">Coach Outlet</a></h1> More on the CelticsKeep on top of the Green throughout the offseason with ESPNBoston. <a href=""title="Coach Outlet">Coach Outlet</a> The Legends have posted records of 24-26 in both of their D-League seasons to date Coach Outlet Online while also serving as a consultant to Mavericks owner Mark Cuban after a seven-year run in Dallas
django A123456a

Reply to this comment

author avatar Aw ertysdfsdf
14th Apr 2014 (#)
fre socer predictions

Reply to this comment

author avatar A
25th May 2015 (#)

Reply to this comment

Add a comment
Can't login?