Open all External links in a New Tab in WordPress without plugins
Today tutorial, we are going to see about How to Open all External links in a New Tab in WordPress without plugins.Recently one of our Blog reader asking me is there any way to Open all External links in new tabs in WordPress without a plugin.After Some research Finally, I got the script for open the External links in a New Tab.
I got this Script from Mybloggertricks.com from the post Stop all Internal Links From Opening In New Window thanks to Mohammed for this awesome script for WordPress.
Feature’s
- Open all External link in New tabs
- commenter’s link also open in new tabs
- support both HTTP and https
Any SEO Benefits?
Here what the Developer said about this script.
How to Open External Links in a New Window in WordPress?
Method1
- If your Have theme Having a Header and Footer Code Option then Just copy the below Javascript and paste it on your Footer script box.
<script type='text/javascript'> //<![CDATA[ //Open External Links in New Window - Plugin by STCnetwork.org $(document).ready(function(){ $("#content a[href^=http], #content a[href^=https]") .each(function(){ if (this.href.indexOf(location.hostname) == -1){ $(this).attr({ "target":"_blank" })} if (this.href.indexOf(location.hostname) != -1){ if ( $(this).attr('target') == '_blank') {$(this).attr("target", "");}} });}); //]]> </script>
- If your WordPress theme does Not Have this Feature then Install Insert Header and Footer Code Plugin to add this script.
Method 2
- Add this script to your theme’s functions.php file by using wp_footer method
- For developer’s try this below format or add this on theme options panel with Enable and disable feature
add_action( 'wp_footer', 'awts_openlinks_innnewtab' ); // Write our JS below here function awts_openlinks_innnewtab() { ?> <script type='text/javascript'> //<![CDATA[ //Open External Links in New Window - Script by STCnetwork.org $(document).ready(function(){ $("#content a[href^=http], #content a[href^=https]") .each(function(){ if (this.href.indexOf(location.hostname) == -1){ $(this).attr({ "target":"_blank" })} if (this.href.indexOf(location.hostname) != -1){ if ( $(this).attr('target') == '_blank') {$(this).attr("target", "");}} });}); //]]> </script> <?php }
- That’s all Now the External links are in the Post will be Automatically Opened in a New Tab
Note
Currently, This script Works only on External Link attached on WordPress Posts.
If you have any doubt in this tutorial please feel free to comment here our team will help you.
Your feedback helps us improve Allwebtuts.com