Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
    
Probably the worst “solution”: You cannot update WordPress anymore without losing the change. –  toscho Nov 11 '12 at 14:39
add comment

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');
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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