Open all External links in a New Tab in WordPress without plugins


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.Here is Solution for this Today Afternoon I research about this and Finally I got the script for this.

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.

 

Open all External links in New Tab WordPress without plugins

How to Open External Links in a New Window in WordPress?

Method1

  • If your Have theme Have an Insert header and Feature Option means Just copy the below Javascript and paste it on your Header 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>

Method 2

  • Add this script to your theme’s functions.php file by using wp_head method
  • For developer’s try this below format or add this on theme options panel with Enable and disable feature
add_action( 'wp_head', '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 Now Open all External links in a New Tab in WordPress without plugins

This Script was developed by Mohammad Mustafa Ahmedzai (STCnetwork.org).

we give the proper credits to developer and admin

If you have any doubt in this tutorial please feel free to comment here our team will help you.

 


Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com



12 Shares
+11
Tweet
Share9
Pin1
Stumble1