2

When adding a link in the WP visual post editor you have to click the settings after adding the link and then click the Open link in a new tab to have it add a link with a target in a new window/tab.

I am looking for a way to have this automatically checked by default. Is there a filter or JavaScript code to do this?

enter image description here

2 Answers 2

2

Add this function in your theme's functions.php

function my_enqueue($hook) {
    if ('post.php' != $hook ) {
        return;
    }
    wp_enqueue_script('my_custom_script', get_template_directory_uri() . '/js/myscript.js');
}

add_action('admin_enqueue_scripts', 'my_enqueue');`

In myscript.js place this code

jQuery(document).ready(function(){
  jQuery('#wp-link-target').prop("checked", true);
})

It worked for me.

2
  • Thanks I was thinking this might be the only way to do it as I found the WordPress file after posting this that creates that form and saw that it does not fire any hook or filters. Thanks
    – JasonDavis
    Jun 25, 2016 at 17:14
  • Where the myscript.js part can be found? Mar 5 at 7:49
0

The accepted answer didn't work for me so i digged a bit and found an event that is triggered when the link popin is displayed. Keep the PHP and replace the JS with this.

    $(document).on('wplink-open', function() {
        if ($('#wp-link-url').val() === '') {
            $('#wp-link-target').prop("checked", true);
        }
    });

I didn't find a way to check if the link is new or edited. So i added a check for the URL, if it's empty we can safely assume it's a new link. Hope that help someone.

2
  • Where does the 2nd part of the code go? Mar 5 at 7:58
  • 1
    What second part ? You put the same PHP than in the accepted response and in myscript.js you put the code above. Mar 7 at 10:30

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.