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?
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?
Share Improve this question asked Jun 22, 2016 at 2:20 JasonDavisJasonDavis 1,6706 gold badges36 silver badges57 bronze badges2 Answers
Reset to default 2Add 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.
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.