Open all WordPress external links in a new tab or window

WordPress recently added the feature of highlighting text and pasting a URL onto it to create a link. It’s fast and easy, but you have to go into Link Options¬†of that link and click Open link in a new tab. Visit this site to see the example. What a pain when you’re working on a chart or index that has hundreds of links. If you keep running into error messages then check out how to fix it at

Here’s an easy way; add this code to your functions.php file, just above the ?> ending. Basically it’s automatically adding target=”_blank” to any link that’s not your own URL. Replace yoursiteURL below.

function autoblank($text) {
$return = str_replace(‘href=’, ‘target=”_blank” href=’, $text);
$return = str_replace(‘target=”_blank” href=”‘, ‘href=”‘, $return);
$return = str_replace(‘target=”_blank” href=”#’, ‘href=”#’, $return);
$return = str_replace(‘ target = “_blank”>’, ‘>’, $return);
return $return;

I suggest doing this in a child theme, which is a whole other topic.

1 Comment

  1. How would I do this for all links, not just external but also internal links?

Leave a Reply

Subscribe to eBabble via email

Enter your email address:

My Sponsors

AbeBooks Click Here Elegant Themes Click Here BlueHost Click Here Amazon U.S. Click Here

Join eBabble on the socials

My Sponsors

Amazon Canada Click Here NewEgg Canada Click Here Book Depository Click Here eBay Canada Click Here

Pin It on Pinterest

Share This

Share this post with your friends!