WordPress php function to open all outbound links in new tab

Who wants a virtual warm chocolate chip cookie?

I’m looking for a WordPress php function to open all outbound links in new tab.

Read More

I have found a couple of function solutions similar to the one below, but they only work for posts/pages and not for items hard-coded into the theme (like social media buttons):

/* OPEN ALL OUTBOUND LINKS IN NEW TAB */
function autoblank($text) {
$return = str_replace('href=', 'target="_blank" href=', $text);
$return = str_replace('target="_blank"
href="http://csihealth.lenadev.com',
'href="http://csihealth.lenadev.com', $return);
$return = str_replace('target="_blank" href="#', 'href="#', $return);
$return = str_replace(' target = "_blank">', '>', $return);
return $return;
}
add_filter('the_content', 'autoblank');
add_filter('comment_text', 'autoblank');

Is there a way to alter this so it works for all outbound links? Not just posts/pages?

Related posts

Leave a Reply