简体   繁体   中英

WP Media Library/Uploader dont returns dynamic HTML/DOM

the sample is from Display Media Uploader in Own Plugin on Wordpress 3.5 :

<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();
});

The problem is about

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

dont returns DYNAMIC html. I've tried code like this:

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

but not working...

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.

I fought with what I think was a similar problem. Part of the trouble was that upon upload, the first attachment object is empty and causes an error.

Here is my code example that outputs HTML as I need it on my site:

// When images are selected, place IDs in hidden custom field and show thumbnails. file_frame.on( 'select', function() {

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);

});

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM