How to Open Links in a New Tab

Lots of plugins are available to open links in a new tab but every time using a plugin is not good. So here I am giving you a simple and easy solution to open link in a new tab using coding. Just follow the instructions given below:-

  • As you have external links in your site and you want to open them in a new tab by clicking on them. For that you need to use a simple code in your header.php file just above the < /head> mentioned below:-

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

    Write your site URL in place of “SITE URL”.

  • Another one is for a particular link in your site. If you want to open a particular link of your site in a new tab. For that use a simple code in your header.php file just above the < /head> mentioned below:-

  • <script type="text/javascript">// <![CDATA[
    jQuery(document).ready(function($){
        $('#ClASS OF LINK').each(function(){
            if( $(this).attr('href') && 0 != $(this).attr('href').indexOf('#') ) {
                $(this).attr('target', '_blank');
            }
        });
    });
    // ]]></script>

Write the particular class of link in place of “#ClASS OF LINK”.

We are always available to solve any query related to the plugin. Hopefully, this post is helpful to you. For more queries visit our support forum.