2

该示例来自Wordpress 3.5 上 Own Plugin 中的 Display Media Uploader

<script type="text/javascript">
var file_frame;

jQuery('.button-secondary').live('click', function( event ){

    event.preventDefault();

    if ( file_frame ) {
        file_frame.open();
        return;
    }

    file_frame = wp.media.frames.file_frame = wp.media(
        {
            title: 'Select File',
            button: {
                text: jQuery( this ).data( 'uploader_button_text' )
            },
            multiple: false
        }
    );

    file_frame.on('select', function() {
        attachment = file_frame.state().get('selection').first().toJSON();
        jQuery('#IMGsrc').val(attachment.url);
    });

    file_frame.open();
});

问题是关于

file_frame.on('select', function() {...});

不返回动态 html。我试过这样的代码:

jQuery(document).on('select', file_frame, function() {...});
jQuery(document).on('select', file_frame.el, function() {...});

但不工作...

4

2 回答 2

0

The window.send_to_editor(html) is called for compatibility reasons even in the new uploader, but I don't think that will work when called this way. You can use the attributes of the object that you selected, and create a link/image html.

于 2013-03-08T04:15:01.840 回答
0

我与我认为类似的问题作斗争。部分问题在于上传时,第一个附件对象为空并导致错误。

这是我的代码示例,可在我的网站上根据需要输出 HTML:

// 选择图像时,将 ID 放置在隐藏的自定义字段中并显示缩略图。file_frame.on('选择',函数(){

var selection = file_frame.state().get('selection');

// Show Thumbs
var attachment_thumbs = selection.map( function( attachment ) {
  attachment = attachment.toJSON();
  if( attachment.id != '' ) { return '<img src="' + attachment.sizes.thumbnail.url + '" id="id-' + attachment.id + '" />'; }
}).join(' ');
$('#images-feedback').show();
$('#thumbs').html(attachment_thumbs);

});

希望这可以帮助!

于 2013-03-21T00:31:35.510 回答