• Is it possible to configure my site so that all links to external sites open in a new window or browser (but internal links continue to open in the same window or tab)? I know how to do it manually, but I have a few hundred posts, so that’d be super time consuming. Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Those links are in the posts itself?

    Thread Starter Z_Everson

    (@z_everson)

    Thanks for the quick response. Yes, they are links in the posts.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    The content in the post can be filtered and all of those links can be found and edited via that filter to add target="_blank".

    I can’t find it now but you can do that via a small plugin easily enough. Tonight I’ll post the pastebin.com link for that plugin. It’s come up before. ??

    So https://zacheverson.com/ should be excluded from target="_blank"? Any other domains you want to exclude?

    Thread Starter Z_Everson

    (@z_everson)

    Great, thanks! No, zacheverson.com is it. Thanks again!

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    One of the coolest features of WordPress is that you can filter items in your posts and this is a modification of something I whipped up before (and re-use all the time).

    Try this plugin. Create a file in your wp-content/plugins called target-blank.php and put these contents into that file.

    <?php
    /*
    Plugin Name: Add target="_blank" to external links
    Description: This will filter links in posts and pages and add target="_blank" to those links
    Version: 1.0
    Author: Jan Dembowski
    Author URI: https://blog.dembowski.net/
    */
    
    add_filter( 'the_content' , 'mh_add_blank' );
    // add_filter( 'comment_text' , 'mh_add_blank' );
    
    function mh_add_blank( $content ) {
    
    // Regex to put all <a href="https://some-url-here/path/etc" into an array
    $mh_url_regex = "/\<a\ href\=\"(http|https)\:\/\/[a-zA-Z0-9\-\.]+[a-zA-Z]{2,3}.*\"[\ >]/";
    
    preg_match_all( $mh_url_regex , $content, $mh_matches );
    
    // Go through that array and add target="_blank" to external links
    for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
            {
            $mh_old_url = $mh_matches[0][$mh_count];
            // $mh_new_url = str_replace( '" ' , '" target="_blank" ' , $mh_matches[0][$mh_count] );
            $mh_new_url = str_replace( '">' , '" target="_blank">' , $mh_matches[0][$mh_count] );
    
            // Array of destinations we don't want to apply the hack to.
            // Your home URL will get excluded but you can add to this array.
            // Partial matches work here, the more specific the better.
    
            $mh_ignore = array(
                    home_url( '/' ),
                    'www.ads-software.com/'
                    );
    
            // Make the substitution on all links except the ignore list
            if( !mh_array_find( $mh_old_url , $mh_ignore ) )
                    $content = str_replace( $mh_old_url  , $mh_new_url , $content );
            }
    
    return $content;
    }
    
    // Only see if the array element is contained in the string
    function mh_array_find( $needle , $haystack ) {
            if(!is_array($haystack)) return false;
            foreach ($haystack as $key=>$item) {
                    // See if the item is in the needle
                    if (strpos($needle, $item ) !== false) return true;
            }
            return false;
    }

    https://pastebin.com/7dXbUNGw

    Once you’ve saved that file go to your WordPress plugins and activate the plugin called Add target="_blank" to external links. That should make all links in your post open in a new window or tab provided that link does not point to your own site.

    If you have additional server names you want to exclude from this then add that to the array $mh_ignore and those will get excluded too.

    If anything goes really wrong then just delete that target-blank.php file.

    Thread Starter Z_Everson

    (@z_everson)

    Great, thanks Jan! I’ll give it a shot tonight.

    Hey I just tried this and it’s not working. I am testing it on links in my bbpress forum replies. Is there anything I can do to make it work inside bbpress and buddypress?

    Yep it’s not working for me either. If I find a solution I will post it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Change all external links to open in new window or tab’ is closed to new replies.