This snippet makes all external links on your site open in a new tab. It also adds the rel="noopener noreferrer" attribute for better security and performance.
Add this code with a snippets plugin right before the closing </body> tag.
jQuery(document).ready(function ($) {
$('a[href^="http"]').each(function () {
if (this.hostname !== window.location.hostname) {
$(this).attr('target', '_blank').attr('rel', 'noopener noreferrer');
}
});
});
Some solutions only work on the live site. Always publish and test after each change, as results may not appear in the editor.