How to Automatically Open External Links in a New Tab on WordPress Website Using Simple Script

Posted by

Table of Contents

In this video, I will show you two methods to automatically open external links in a new tab on your website. The first method is the manual method. The second method is the automatic method, where you can add a script to your website that will open all external links in a new tab. This will save you time when creating new blogs on your website. I will demonstrate both methods and provide the necessary code in the video description.

Script To Add

<script>
    (function() {
        const anchorTags = document.querySelectorAll('a');
        anchorTags.forEach(link => {
            if (link.hostname !== window.location.hostname) {
                link.setAttribute('target', '_blank');
                link.setAttribute('rel', 'noopener noreferrer');
            }
        });
    })();
</script>

Join Our Community

Subscribe to Go With Blogs for regular updates.

You may be interested in