php - Open all external links in new window - need help with the code

admin2025-01-07  5

I'm using this code to open all external links in new window (site is based on Wordpress):

/* OPEN ALL OUTBOUND LINKS IN NEW TAB */
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;
}
add_filter('the_content', 'autoblank');
add_filter('comment_text', 'autoblank');

Unfortunately it's not perfect because it also opens internal links in new window (only if these are placed in posts or pages - menu items etc. open normally). Does anyone have an idea how to modify this code to not open internal links in new window?

I'm using this code to open all external links in new window (site is based on Wordpress):

/* 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://example.com',
'href="https://example.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');

Unfortunately it's not perfect because it also opens internal links in new window (only if these are placed in posts or pages - menu items etc. open normally). Does anyone have an idea how to modify this code to not open internal links in new window?

Share Improve this question asked Mar 13, 2019 at 22:27 kacper3355kacper3355 772 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Thanks for that, Jurgen, but there was an error in the code that I fixed:

$('a').each(function() {

    /** Get the current hostname */
    var currentHostname = new RegExp(window.location.host);

    /** Check if the href attribute of the link has a different hostname than the current site */
    if (!currentHostname.test(this.href)) {

        $(this).on('click', function(event) {

            event.preventDefault();
            event.stopPropagation();

            /** Create a URL object for the link */
            var url = new URL(this.href);

            /**
             * Check if the link is an http or https protocol link
             * otherwise just open all other link protocols (tel, mailto)
             */
            if (url.protocol == 'http:' || url.protocol == 'https:') {
                window.open(this.href, '_blank');
            } else {
                window.location.replace(this.href);
            }

        });
    }

});

Way to complicated to do this with php in WordPress, I'd advice on doing this on the frontend with a bit of jquery magic. I wrote a tiny jQuery plugin that opens all external links (all links with another domain/host name than the current site) in a new window.

Check out the plugin: https://www.npmjs.com/package/jquery.jold.external-hrefs

To do this manually, without the plugin is pretty simple too:

$('a').each(function() {

    /** Get the current hostname */
    var a = new RegExp('/' + window.location.host + '/');

    /** Check if the href attribute of the link has a different hostname than the current site */
    if( !a.test(this.href) ) {

        $(this).on('click', function(event) {

            event.preventDefault();
            event.stopPropagation();

            /** Create an url object for the link */
            url = new URL(this.href);

            /**
             * Check if the links is a http protocol link
             * otherwise just open all other link protocols (tel, mailto)
             */
            if (url.protocol == 'http:' || url.protocol == 'https:') {
                window.open(this.href, '_blank');
            } else {
                window.location.replace(this.href);
            }

        });
    }

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254180a183.html

最新回复(0)