jQuery(document).ready(function ($) { $(document).on('click', "button[class*='upload_']", function (e) { e.preventDefault() var $button = $(this) if (file_frame) { file_frame.open() return } var file_frame = (wp.media.frames.file_frame = wp.media({ library: { type: 'image' }, multiple: false })) file_frame.on('select', function () { var attachment = file_frame.state().get('selection').first().toJSON() $button.siblings('input').val(attachment.url).change() }) file_frame.open() }) })