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

wordpress.stackexchange https://wordpress.stackexchange.com/questions/7785

  •  16-10-2019
  •  | 
  •  

Question

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.)

Was it helpful?

Solution

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.

OTHER TIPS

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

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).

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');

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top