Home > Tips & Tricks > Open All External Links in New Window

Open All External Links in New Window

If you are searching for a code snippet that will help you to make functionality for external links open in a new window? Then you are at the right place. We will show you to make it easy with this code trick.

We will create an add_action() hook function to enqueue the custom javascript file and load the file in the header.

Add the above code in the functions.php file.

In the above code, we load the js file. But we don’t have this file yet. So we will create this file and will create the function.

So, first, create the js folder in your active theme and then create the external-links.js file. Now we have a file and we will create a jQuery function. We will check all links on the website and filter the external links and open them in a new window.

Add the following jQuery code in the newly created file (external-links.js)

The above code will find the external links and will add the target attribute to make it open in a new tab.

That’s it.

Tip: Add noopener and noreferrer

If you also want to add noopener and noreferrer attributes to those external links then you can do this by adding rel attribute.

Being Tricky 😉

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment