WP Media插入文本编辑器

时间:2018-11-08 09:45:02

标签: wordpress

我有这段代码可以在单击自定义按钮后打开Wordpress Media Uploader,并且从上载图像到选择图像,一切工作都可以进行,但是如何将图像/附件发送到文本编辑器

jQuery(document).ready( function($){

var mediaUploader;

$('#_button').on('click',function(e) {
    e.preventDefault();
    if( mediaUploader ){
        mediaUploader.open();
        return;
    }

    mediaUploader = wp.media.frames.file_frame = wp.media( {
        title       : 'My Custom Library',
        multiple    : false,
        library     : { type : 'image' },
        button      : { text : 'Select Image' },
        frame      : 'post',
        state       : 'insert',

    } );


    mediaUploader.on('insert', function() {
        var attachment = mediaUploader.state().get('selection').first().toJSON();

        //WHAT TO DO HERE TO SEND THIS TO TEXT EDITOR??????

    });

    mediaUploader.open();

}); });

1 个答案:

答案 0 :(得分:0)

自己找到答案
  

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/js/media-editor.js#L852

        mediaUploader.on('insert', function() {
        var embed = mediaUploader.state().get( 'selection' ).first().toJSON();
        _.defaults( embed, {
            title:   embed.url,
            linkUrl: '',
            align:   'none',
            link:    'none'
        });

        if ( 'none' === embed.link ) {
            embed.linkUrl = '';
        } else if ( 'file' === embed.link ) {
            embed.linkUrl = embed.url;
        }

        wp.media.editor.insert( wp.media.string.image( embed ) );
    });