Open all links in a new tab or open all external links of a website in a new tab – Couple of days back, I have created a job portal website like Sarkari Result. After completion of the design, I met a requirement to open all internal and external links of my website in a new window.

So, I notched my head and started thinking about how this requirement can be met. Because I am in this blogging business for the last two years and all the time, we have tried opening a single link in a new tab.

As you know opening a single link in a new tab or new window is like a bread and butter. We just have to change our code a bit.

For example – See the HTML code given below. The keyword (target=”_blank”) fits perfect and do the needful to open a link in a new tab.

<a href="https://okeyravi.com" target="_blank">This link will open in new window/tab</a>

Other than this if you are using Gutenberg Editor, Classic Editor, Elementor Page Builder, Beaver Builder Page builder or any other page builders then you will get a lot of customization options for a link.

So, now the question comes what if we have to open every link of a WordPress website in a new tab or if we only want to open only external links.

Code to open all links of a website in New Tab/Window

If you want to open all links irrespective of they are internal or external links in a new tab or new window then try the code given below.

Open your header.php file and add this code just above </head>. It will do the rest.

<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function($){
    $('a').each(function(){
        if( $(this).attr('href') && 0 != $(this).attr('href').indexOf('#') ) {
            $(this).attr('target', '_blank');
        }
    });
});
// ]]></script>
Opening all website links in a new tab in wordpress
Opening all website links in a new tab in WordPress

Code to open only external links of a website in New tab

If you have such requirements in which you want only external or outbound links to be open in a new tab or new window. Then this code will be helpful.

Open your header.php file and add this code just above </head>

<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function($){
    $('a[href]:not([href^="ENTER your Main Website URL here"]):not([href^="#"]):not([href^="/"])').attr( 'target', '_blank' );
});
// ]]></script>

Note – In the above code in place of “ENTER your Main Website URL here”, use your main domain link

Conclusion – Open all links or Only external links in new window

So, accordingly based on the requirements or our website we may have to navigate each member to a new tab on a link click or only if they click on external links.

We have provides codes for both cases. Make sure to use them and leave a comment below after using it, if it is working fine.

Thanks for reading this post.

Make sure to rate the post below. Have a good day!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

We are sorry that this post was not useful for you!

Let us improve this post!

Open All Links/External Links of a website in New Tab/Window
Spread the love
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Okey Ravi

I am a System Engineer at Infosys Limited, Blogger, YoutTube Creator, SEO and WordPress Expert. I'm very enthusiastic to "update myself with the latest technologies" & the things which I learned, I deliver to you with this blog. So, be in touch & Follow Okey Ravi socially to be updated with the trends.

4 thoughts on “Open All Links/External Links of a website in New Tab/Window

  • September 1, 2019 at 1:31 pm
    Permalink

    Hi Ravi,

    Thanks for sharing this awesome Post. It Worked for my website Sarkarijobpost.com.

    Reply
    • September 1, 2019 at 2:30 pm
      Permalink

      Hey Ayush, Welcome. I am glad to listen that it worked for you.

      Reply
    • September 2, 2019 at 11:24 pm
      Permalink

      Hi Ravi,
      I read this blog. But opening a new link in the new tab or any external link in a new tab, how can it help in terms of SEO?
      Regards,
      Sneha

      Reply
      • September 3, 2019 at 9:26 pm
        Permalink

        Hi Sneha,

        I have not mentioned anything about SEO in this post. Opening a link in a new window or new tab is requirements of some websites. Nothing else. There will not be effor on SEO due to this.

        Hope you get it. Thanks for asking.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *