2

I am searching for an option to open every external links from my website (wordpress-blog) to any other sites automatically in a new window. Is that with css or html possible without doing it 1000 times manually via hand as "target _blank"?

Thank you so much!

PS: sry for my bad english, I am no native speaker :(

  • 1
    If you don't want to add target="_blank" to your external links then you'll need javascript for that. – Danield Jan 26 '17 at 13:37

3 Answers3

2

Yes, You can use Open external links in a new window plugin.

It will be helpful to open all or specific external links in a new window.

Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
2

Put this code in your theme functions.php file.

function cdx_handel_external_links() {
    ?>
<script type="text/javascript">
( function( $ ) {

    $("a[href^=http]").click(function(){
      if(this.href.indexOf(location.hostname) == -1) {
         $(this).attr({
            target: "_blank"
         });
      }
    })

   //Add Nofollow
   $("a").each(function(){
    if(this.href.indexOf('nytimes.com') >0 ){
        $(this).attr({
            rel: "nofollow"
         });
    }
   });

} )( jQuery );
</script>
   <?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
Codextent
  • 400
  • 5
  • 7
  • OMG, that really works ^^ Unfortunately I am really a totally coding-noob and so I hope with this code never could happen a technical problem or something else like that? I just put this code to the end of functions.php and yes, it works! thank you so much. And this is working on every browser and smartshones (io, android etc) as well, right? Really thank you! –  Jan 28 '17 at 14:00
  • Moreover: I am searching for an option to set automatically all external links to a certain domain (and all of its subpages) to "nofollow". Maybe this is possible with the functions.php file too? Maybe even with your code, "just" to add something? Would be great if you could help me out with that too. THX a bunch!! –  Jan 28 '17 at 14:07
  • Could you give me an example of your nofollow requirement ? – Codextent Jan 29 '17 at 05:17
  • Sure :) For example all links to https://www.nytimes.com/ and all of its subpages like http://www.nytimes.com/2017/01/28/us/refugees-detained-at-us-airports-prompting-legal-challenges-to-trumps-immigration-order.html and so on should have a nofollow automatically. In my case I will use it for an onlineshop but dont want to public the url here. Thank you a very lot! –  Jan 29 '17 at 13:07
  • Hi, I have modified the code.You can change the "nytimes.com" inside the code to any other domain for auto add rel=nofollow. Please choose this as your answer so that others can use this code for same situation :) – Codextent Jan 30 '17 at 10:13
  • You are... just awesome! I clicked on the green hook left besides your code, is that right? One more question: the code writes the nofollow in the source of the html site, but not the new-window-function _blank. Why? And will every browser accept this kind of code? THANK YOU SO MUCH, god bless you - I pray for it :) –  Feb 01 '17 at 12:37
  • Ah, one question again: If I use caching engine plugins like https://wordpress.org/plugins/wp-super-cache/ , there is no problem with the served static html files and the pnp functions of new window + nofollow from your code? –  Feb 01 '17 at 13:35
  • Unfortunately today it does not longer working in some browsers :( F.e. in Iexplore it works but not in Firefox. Why? –  Nov 08 '17 at 17:16
1

If you put the following in the head tag of your HTML, any href tag without a target should open in a new window:

<head>
    <base target="_blank">
</head>
Sean _space
  • 112
  • 7
  • But then it will be open each of my own internal links in a new window as well, wont it? –  Jan 28 '17 at 13:29