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

Read More
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…

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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!