Search code examples
jquerywordpressjquery-file-upload

WordPress media Uploader jQuery multi button How to get different value


WordPress media Uploader jQuery multi button get only one value( Get same value to all button ) I don't know how to get exact value for using multi button? what mistake i did?.

My jQuery code:

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

var mediaUploader;

$('.upload-button').on('click',function(e) {
    e.preventDefault();
    var buttonID = $(this).data('group');

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

  mediaUploader = wp.media.frames.file_frame =wp.media({
    title: 'Choose a Hotel Picture',
    button: {
        text: 'Choose Picture'
    },
    multiple:false
  });

  mediaUploader.on('select', function(){
    attachment = mediaUploader.state().get('selection').first().toJSON();
    $('#profile-picture'+buttonID).val(attachment.url);
    $('#profile-picture-preview'+buttonID).css('background-image','url(' + attachment.url + ')');
  });
  mediaUploader.open();
}); });

My PHP/HTML code:

<input type="button" class="button button-secondary upload-button" value="Upload Profile Picture" data-group="1">
<input type="hidden" name="profile_picture1" id="profile-picture" value="'.$picture1.'">
<input type="button" class="button button-secondary upload-button" value="Upload Profile Picture" data-group="2">
<input type="hidden" name="profile_picture2" id="profile-picture" value="'.$picture2.'">

Any help, as always, is greatly appreciated.


Solution

  • You can use below updated HTML code:

    <input type="button" class="button button-secondary upload-button" value="Upload Profile Picture" data-group="1">
    <input type="hidden" name="profile_picture1" id="profile-picture1" value="'.$picture1.'">
    <input type="button" class="button button-secondary upload-button" value="Upload Profile Picture" data-group="2">
    <input type="hidden" name="profile_picture2" id="profile-picture2" value="'.$picture2.'">