WordPress.org

Ready to get started?Download WordPress

Forums

Multiple media upload frames (3 posts)

  1. ronnyrook
    Member
    Posted 1 year ago #

    Hello WordPress users,

    I'm stuck with a problem while building my WordPress plugin.

    I want to use 2 Media Upload iframes in on one plugin page.
    I tried multiple scripts. Curretly I'm using the code from a tutorial. But I also used this code.

    var file_frame;
    
        jQuery('.upload_button').live('click', function( event ){
    
            var clicked = $(this);
    
            event.preventDefault();
    
            if ( file_frame ) {
                file_frame.open();
                return;
            }
    
            file_frame = wp.media.frames.file_frame = wp.media({
                title: jQuery( this ).data( 'upload_image' ),
                button: {
                    text: jQuery( this ).data( 'uploader_button_text' )
                },
                multiple: false
            });
    
            file_frame.on( 'select', function() {
                attachment = file_frame.state().get('selection').first().toJSON();
    
                clicked.closest('p').find('.upload_image').val(attachment.url);
            });
    
            file_frame.open();
        });

    But this ain't work for me.
    Im using 2 fields:

    <p><input type="text" name="upload_image" class="upload_image" />
           <input type="file" name="image" class="upload_button" /></p>

    &

    <p><input type="text" name="edit_upload_image" class="upload_image" value="<?php echo $edit[0]->image ?>" />
            <input type="file" name="edit_image" class="upload_button" /></p>

    With the first field all the code works fine.
    But at the second field the code is not sending the url to my input field.

    The iframe works fine.
    I can select and upload al the images I want, but when I press 'Select Image' the url is not sended to the input area.

    Someone recognized this problem?
    Thanks :)

  2. ronnyrook
    Member
    Posted 1 year ago #

    Im still stuck on this problem..
    Someone who can help me out?

  3. Toemat
    Member
    Posted 1 year ago #

    Hey ronnyrook,
    I had the same problem, it's easily fixed by removing these lines:

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

    The problem with this is that each time the window is opened it will create a new instance of it (I don't think there is a method of removing them), so this could pose performance problems if you have a ton of them on the same page. In my case there's only two spots and it's unlikely the 'Choose' buttons will be clicked a bunch of times, so this works!

Topic Closed

This topic has been closed to new replies.

About this Topic