我有这个代码,如果你使用一个小部件,它就可以完美地工作。但是,如果创建另一个小部件,上载程序将覆盖这两个小部件的设置。
有人知道如何修改此脚本以允许多次使用吗?
HTML
<p>
<label for="<?php echo $this->get_field_name( \'image_url\' ); ?>"><?php _e( \'Category Image:\', \'text-domain\' ); ?></label>
<input name="<?php echo $this->get_field_name( \'image_url\' ); ?>" id="<?php echo $this->get_field_id( \'image_url\' ); ?>" class="widefat img custom_media_url" type="text" size="36" value="<?php echo esc_url( $image_url ); ?>" />
<input class="custom_media_upload" id="custom_media_button" type="button" value="<?php esc_attr_e(\'Upload Image\', \'text-domain\'); ?>" />
</p>
Javascript:
jQuery(document).ready( function($) {
function media_upload(button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$(\'body\').on(\'click\', button_class, function(e) {
var button_id =\'#\'+$(this).attr(\'id\');
var self = $(button_id);
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(button_id);
var id = button.attr(\'id\').replace(\'_button\', \'\');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$(\'.custom_media_url\').val(attachment.url);
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
media_upload(\'.custom_media_upload\');
});