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.

Introduction

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.

Tags

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

Comments

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, http://www.guccishoes-factory.net/

Coach Factory Outlet, http://www.coachfactories.com/

Ralph Lauren Outlet Online, http://www.laurenpoloralph.com/

North Face Jackets Clearance, http://www.north-clearances.com/

MCM Bags Outlet Online, http://www.mcmsworldwide.com/

Michael Kors Outlet Online, http://www.clickmichaelkors.com/

Coach Outlet, http://coachoutlettusa.tumblr.com/

Coach Purse Outlet Online, http://www.coach-pursesonsale.com/

Louis Vuitton Outlet, http://www.lv-guccishoesfactory.com/

Michael Kors Outlet Online, http://www.michaelkorsoutletsusa.com/

Monster Beats Outlet, http://www.mmonsterheadphones.net/

Coach Factory Store, http://coachfactorys.tumblr.com/

North Jackets Outlet Online, http://www.north-faceoutletonlines.net/

Coach Factory, http://www.bags-outletfactory.com/

Coach Purses Outlet, http://www.newoutletonlinemall.com/

Polo Outlet, http://www.polo-outletstoreonline.com/

Louis Vuitton Outlet Online, http://www.l-vusashop.com/

Ugg Bailey Button Factory, http://auuggboots.tumblr.com/

Coach Outlet Online, http://www.copursesoutletonline.com/

Michael Kors Outlet, http://www.mkoutletcarts.com/

Reply to this comment

author avatar Django
28th Dec 2013 (#)

Celtics president of basketball operations Danny Ainge and Green's agent <a href="http://www.coachoutletonlineua.net/">Coach Outlet Online</a> David Falk, maintained during the lengthy delay that the deal eventually would get done <h1><a href="http://www.coachfactoryoutletfa.net/">Coach Factory Online</a></h1> but wouldn't shed light on what was holding up the process <h1><a href="http://www.coachoutleshome.com/"title="Coach Outlet">Coach Outlet</a></h1> More on the CelticsKeep on top of the Green throughout the offseason with ESPNBoston. <a href="http://www.coachoutleshome.com/"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 http://www.coachoutleshome.com
django A123456a

Reply to this comment

author avatar Aw ertysdfsdf
14th Apr 2014 (#)

http://betprediction.esy.es/futbol-europeo/
fre socer predictions

Reply to this comment

author avatar A
25th May 2015 (#)

http://www.chanelhandbags.net.co/
http://www.chiflatiron.net.co/
http://www.converse.net.co/
http://www.ferragamo.com.co/
http://www.guccihandbags.com.co/
http://www.hollister.us.org/
http://www.thenorthfacejackets.in.net/
http://www.northfaceoutlet.co.com/
http://www.burberryoutlet-online.co.com/
http://www.ralph-lauren.org.uk/
http://www.polo-outlets.com/
http://www.ralphlaurenpolo.in.net/
http://www.insanityworkout.net.co/
http://www.jimmychoo.net.co/
http://www.jordan-shoes.com.co/
http://www.juicycouture.com.co/
http://www.juicycoutureoutlet.net.co/
http://www.kate-spade.com.co/
http://www.katespade-outlet.com.co/
http://www.louisvuitton-outlet.com.co/
http://www.michael--kors.us.com/
http://www.michael-kors-handbags.us.com/
http://www.michael-kors-outlet.us.org/
http://www.michael-kors-outlet-online.us.org/
http://www.michaelkorsoutletonline-sale.us.com/
http://www.northface.us.org/
http://www.oakley-sunglasses.us.org/
http://www.pandorajewelry.com.co/
http://www.pradahandbags.com.co/
http://www.pradashoes.com.co/
http://www.ray-ban-outlet.us.com/
http://www.rayban-sunglasses.us.org/
http://www.replicawatches.us.com/
http://www.swarovskicrystal.com.co/
http://www.thenorthface.us.org/
http://www.tiffanyandco.net.co/
http://www.tiffanyjewelry.us.org/
http://www.true-religion.com.co/
http://www.truereligionjeans.net.co/
http://www.uggaustralia.net.co/
http://www.uggboots.net.co/
http://www.uggbootsclearance.com.co/
http://www.uggsonsale.com.co/
http://www.uggsoutlet.com.co/
http://www.cheap-oakleysunglasses.in.net/
http://www.oakleysunglassescheap.in.net/
http://www.ralphlaurenoutletonline.in.net/
http://www.ralphlauren-outlet.co.com/
http://www.burberryoutlet-online.in.net/
http://www.toms-shoes.com.co/
http://www.toms-outlet.in.net/
http://www.christian--louboutin.in.net/
http://www.tory-burch-outlet.in.net/
http://www.gucci-outlet.in.net/
http://www.longchamp.co.com/

Reply to this comment

Add a comment
Username
Can't login?
Password