How to Open External Link in new Window using Function.php
-
I need to find the code I can use in functions.php that opens external links in the new window. I know there are plugins and javascript, but I already have too many plugins and jscripts loading.
I did find a script that seems to be working (see below), unfortunately, it opens all links in the new window including internal links.
Can someone help me find the code that only opens external links in the new window that can be used within functions.php file?
———opens all links new window—————–
/* OPEN ALL OUTBOUND LINKS IN NEW TAB */
function autoblank($text) {
$return = str_replace(‘href=’, ‘target=”_blank” href=’, $text);
$return = str_replace(‘target=”_blank”
href=”https://bigapplemedia.com’,
‘href=”https://www.bigapplemedia.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’);The page I need help with: [log in to see the link]
- The topic ‘How to Open External Link in new Window using Function.php’ is closed to new replies.