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.