Is there any way to set default for “Insert/Edit Link” to “Open link in new window”?

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.

Leave a Comment

tech