检查是否适合您:输入此代码
jQuery(document).ready( function(){
function media_upload( button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
jQuery(\'body\').on(\'click\',button_class, function(e) {
var button_id =\'#\'+jQuery(this).attr(\'id\');
/* console.log(button_id); */
var self = jQuery(button_id);
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = jQuery(button_id);
var id = button.attr(\'id\').replace(\'_button\', \'\');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
jQuery(\'.custom_media_id\').val(attachment.id);
jQuery(\'.custom_media_url\').val(attachment.url);
jQuery(\'.custom_media_image\').attr(\'src\',attachment.url).css(\'display\',\'block\');
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
media_upload( \'.custom_media_upload\');
});
您可以使用按钮来代替上传链接
<input type="button" value="<?php _e( \'Upload Image\', \'theme name\' ); ?>" class="button custom_media_upload" id="custom_image_uploader"/>
Update:
只要在js中做一些小的更改,您的问题就会得到解决,而不是
jQuery(button_class).click(function(e) {
你必须使用
jQuery(\'body\').on(\'click\',button_class, function(e) {
由于小部件是使用ajax添加的。如果在js中进行类似的更改,那么即使是以前的代码也应该可以工作。
$(\'body\').on(\'click\',\'.custom_media_upload\',function(e) {