-
-
Save SalvatoreNoschese/9057c8e99277d7b60d7f24d300d6301f to your computer and use it in GitHub Desktop.
External Links in New Tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: External Links in New Tab | |
Plugin URI: https://salvatorenoschese.it/mu-plugin-external-links-in-new-tab | |
Description: Opens external links in a new tab. | |
Version: 0.1 | |
Author: Salvatore Noschese | |
Author URI: https://salvatorenoschese.it | |
*/ | |
defined("ABSPATH") || die("Access denied."); | |
add_action('wp_footer', 'custom_external_links_script'); | |
function custom_external_links_script() { | |
?> | |
<script> | |
(function () { | |
var targetBlankPluginLinks = document.querySelectorAll('a[href^="https://"]'); | |
targetBlankPluginLinks.forEach(function (targetBlankPluginLink) { | |
// Check if the link's domain is different from the current page's domain | |
if (targetBlankPluginLink.hostname !== window.location.hostname) { | |
targetBlankPluginLink.setAttribute('target', '_blank'); | |
} | |
}); | |
})(); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment