最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tinymce - Open link in a new tab checked by default when adding a new link in visual post editor

programmeradmin2浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论