1

I almost always select "Open link in new window" when creating an URL/HREF. Is there any way to default this choice.

Even if it requires a small source code change, I think it would be worthwhile (if someone can tell me where that might be.)

  • Where in WordPress you are finding these "Insert/Edit Links" you want to modify? – MikeSchinkel Jan 26 '11 at 7:56
  • @Mike: I assume he is talking about the link button in the visual editor toolbar. It is called "Insert/Edit Link" and has a "Target" option that can be set to "Open link in a new window". – Jan Fabry Jan 26 '11 at 9:17
6

It seems TinyMCE provides no easy setting to select a default value. But there is another backdoor: the external_link_list_url option of TinyMCE can point to an extra Javascript file that will be loaded in the link editor window. You can use it to populate a dropdown of frequent link destinations, but since it is a regular Javascript file we can also drop other content in it. Like code that will change the selected value of the target list dropdown if we are not editing an existing link:

tinyMCEPopup.onInit.add( function() {
    if ( ! tinyMCEPopup.editor.dom.getParent( tinyMCEPopup.editor.selection.getNode(), 'A' ) ) {
        selectByValue( document.forms[0], 'target_list', '_blank' );
    }
} );

You can create a WordPress plugin for this, so it will survive WP updates. Create a new directory under wp-content/plugins/ (call it whatever you like, so you can find it later). Create a PHP file in it (also called whatever you like), with the following contents. You can change the contents of the comment, this will define what you see in the Plugins administration area.

<?php
/*
Plugin Name: WPSE 7785
Plugin URI: http://wordpress.stackexchange.com/questions/7785/is-there-any-way-to-set-default-for-insertedit-link-to-open-link-in-new-window--
Description: Is there any way to set default for "Insert/Edit Link" to "Open link in new window"?  
Version: 1.0
Author: Jan Fabry
*/
add_filter( 'tiny_mce_before_init', 'wpse7785_tiny_mce_before_init' );
function wpse7785_tiny_mce_before_init( $initArray )
{
    $initArray['external_link_list_url'] = plugins_url( 'wpse-7785.js', __FILE__ );
    return $initArray;
}

Now also create a Javascript file in that plugin directory, next to the PHP file. I called it wpse-7785.js, you can choose something else, but be sure to update the name in the plugins_url() call above. Place the contents of the first block in that Javascript file.

Activate the plugin and go to your editor. When you go to the post editor and click the "Edit link" button, the correct value should be set for the "Target" dropdown.

  • This is the first time I've been able to try this - but I'm still too confused. I found this directory: public_html/wp-includes/js/tinymce/plugins and created file wpse07785.js there. But I'm not sure what file to mod for the second code block above (the TinyMCE settings file?). I'm on 3.0.4 by the way. – NealWalters Jan 28 '11 at 3:37
  • @NealWalters: I updated my answer with instructions how to make it a WordPress plugin. Feel free to ask for more info when something is not clear! – Jan Fabry Jan 28 '11 at 6:55
  • Wow! Amazing, elegant, and simple! I'm SO impressed! – NealWalters Jan 30 '11 at 4:27
  • 1
    FYI: this no longer works with TinyMCE 4. – Paul Schreiber Jul 3 '14 at 21:20
3

I followed your instructions and was not working at all, because wordpress changed a bit the id of the link. I fixed myself and I created a plugin.

<?php
/*

 Plugin Name: Default New Window Link Opener
 Plugin URI: https://github.com/eballo
 Description: Plugin for wordpress that allow you to enable the check in the popup "Insert/Edit Link” to open a link in new window by default
 Version: 1.0
 Author: eballo

This plugin is based on the solution made by Jan Fabry and lewayotte in wordpress stackexchange forum.

http://wordpress.stackexchange.com/questions/7785/is-there-any-way-to-set-default-for-insert-edit-link-to-open-link-in-new-wind

*/

/**
 * Sets "Open link in a new window/tab" to checked by default
 */
function wplink_tiny_mce_init()
{
 ?>
    <script type="text/javascript">
    jQuery(function () {
       jQuery('input#wp-link-target').prop('checked',true);
    });
</script>    
<?php
}

add_action( 'before_wp_tiny_mce', 'wplink_tiny_mce_init' );

?>

If someone want my solution it is here working: https://github.com/eballo/DefaultCheckedLink/

UPDATE: Plugin created and published: http://www.wordpress.org/plugins/default-new-window-link-opener

  • I want to publish this plugin in wordpress (and i will maintain it) with this content. is it ok Jan Fabry ? I say so because I'm mentioning you in the plugin. Thanks in advance. – eballo Dec 18 '16 at 9:07
  • Plugin created and published: wordpress.org/plugins/default-new-window-link-opener – eballo Dec 18 '16 at 10:07
2

This worked for me...

/*
 * Sets "Open link in a new window/tab" to checked by default
 */
function ahu_after_wp_tiny_mce() {
    ?>
    <script type="text/javascript">
    jQuery( function() {
        jQuery( 'input#link-target-checkbox' ).prop( 'checked', true );
    } );
    </script>    
    <?php
}
add_action( 'after_wp_tiny_mce', 'ahu_after_wp_tiny_mce' );

I stuck this in a PHP file in the wp-content/mu-plugins directory (which you need to create if it doesn't exist).

0

You can create a filter for the hook edit_tag_link and munge the link to taste. This is one of a huge number of undocumented (or under documented) hooks in WP.

I would try something like (warning: untested):

function edit_tag_link_new_window($content) {
    $content = preg_replace('/href/', 'target="_blank" href', $content);
    return $content;
}
add_filter('edit_tag_link', 'edit_tag_link_new_window');
  • Would this always override your setting? What if you want one link that does not open in a new window? – Jan Fabry Jan 26 '11 at 9:18
  • @Jan: Good question. I answered this at 1am (insomnia) and I don't think I read the question correctly. I thought he was asking about the "Edit" link you get on a page when you are logged in as an admin. – Peter Rowell Jan 26 '11 at 16:45
0

I tried the Plugin Name: WPSE 7785 idea above - the Plugin installed successfully but didn't change the default setting for "Open link in a new window/tab" - which I believe is the intention of the original request.

I did a bit more searching/testing and found that the control for this is actually in the following WordPress file: /wp-admin/includes/internal-linking.php

Around line 85 is the following:

<input type="checkbox" id="link-target-checkbox" tabindex="30" /> <?php _e( 'Open link in a new window/tab' ); ?></label>

I simply added "checked" to the input:

<input type="checkbox" id="link-target-checkbox" checked tabindex="30" /> <?php _e( 'Open link in a new window/tab' ); ?></label>

The drawback is that this file is overwritten when WordPress is updated but for now I have a "readme.txt" file in my WP folder for future reference/reminder. I'm sure there is a way to create a hook outside of the admin folder that could be used to control this and not be affected by updates but I'm not at that level of WP skill yet.

  • Probably the worst “solution”: You cannot update WordPress anymore without losing the change. – fuxia Nov 11 '12 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.