admin管理员组文章数量:1122832
I am trying to implement a custom WP Media uploader within a plugin. I want multiple images to be uploaded also.
I have the following javascript to open the media uploader. The modal opens fine and I am able to select existsing media. I am also able to upload new media sucessfully, but when uploading the new media, it will not display within the media uploader. There is a thumbnail of the new image on the right hand side of the uploader but not thumbnail to select within the actual uploader.
jQuery( document ).ready(function( $ ){
var image_uploader;
$('#image-button').click(function(e){
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (image_uploader) {
image_uploader.open();
return;
}
//Extend the wp.media object
image_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: true,
library: {
type: [ 'image' ],
//filterable: 'all',
editable: false,
},
});
//When files are selected, extract information into JSON object
image_uploader.on('select', function() {
var attachments = image_uploader.state().get('selection').map(
function( attachment ) {
attachment.toJSON();
return attachment;
}
);
var i;
// Remove the existing images
$('.item-image').remove();
$('input[name="image_id_array[]"]').remove();
// Add the images to the display area
for (i = 0; i < attachments.length; ++i) {
$('#image-section').before(
'<img src="' + attachments[i].attributes.url + '" class="item-image" id="' + attachments[i].id + '">'
);
}
// Add the inputs for each image to the display area
for (i = 0; i < attachments.length; ++i) {
$('#image-section').before(
'<input type="hidden" name="image_id_array[]" id="image' + attachments[i].id + '" value="' + attachments[i].id + '">'
);
}
});
//Open the uploader dialog
image_uploader.open();
$('#remove-button').removeClass("hidden");
})
})
I have tried refactoring this code, but cannot work out why it is not allowing image display for newly uploaded images. I have tried on a cople of different machines and development sites, but the bug is the same on all of them.
Any help from you awesome guys would be much appreciated.
I am trying to implement a custom WP Media uploader within a plugin. I want multiple images to be uploaded also.
I have the following javascript to open the media uploader. The modal opens fine and I am able to select existsing media. I am also able to upload new media sucessfully, but when uploading the new media, it will not display within the media uploader. There is a thumbnail of the new image on the right hand side of the uploader but not thumbnail to select within the actual uploader.
jQuery( document ).ready(function( $ ){
var image_uploader;
$('#image-button').click(function(e){
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (image_uploader) {
image_uploader.open();
return;
}
//Extend the wp.media object
image_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: true,
library: {
type: [ 'image' ],
//filterable: 'all',
editable: false,
},
});
//When files are selected, extract information into JSON object
image_uploader.on('select', function() {
var attachments = image_uploader.state().get('selection').map(
function( attachment ) {
attachment.toJSON();
return attachment;
}
);
var i;
// Remove the existing images
$('.item-image').remove();
$('input[name="image_id_array[]"]').remove();
// Add the images to the display area
for (i = 0; i < attachments.length; ++i) {
$('#image-section').before(
'<img src="' + attachments[i].attributes.url + '" class="item-image" id="' + attachments[i].id + '">'
);
}
// Add the inputs for each image to the display area
for (i = 0; i < attachments.length; ++i) {
$('#image-section').before(
'<input type="hidden" name="image_id_array[]" id="image' + attachments[i].id + '" value="' + attachments[i].id + '">'
);
}
});
//Open the uploader dialog
image_uploader.open();
$('#remove-button').removeClass("hidden");
})
})
I have tried refactoring this code, but cannot work out why it is not allowing image display for newly uploaded images. I have tried on a cople of different machines and development sites, but the bug is the same on all of them.
Any help from you awesome guys would be much appreciated.
Share Improve this question asked Aug 8, 2023 at 16:24 JadeTechJadeTech 114 bronze badges1 Answer
Reset to default 0Your issue sounds like a visual bug in the WordPress Media Uploader's interface after an image has been uploaded, especially since you mentioned that the image appears in the right-hand preview pane but not as a selectable thumbnail.
Try to force refresh the media frame after upload. You can use the add:attachment
event to force the media frame to refresh when a new attachment is added. Add this right after you define your image_uploader
object:
wp.media.model.Attachments.all.on('add', function(){
image_uploader.uploader.uploader.refresh();
});
I haven't tested this code, so just adjust it so it fits your code.
本文标签: plugin developmentWordpress Media Uploader not displaying image that has just been uploaded
版权声明:本文标题:plugin development - Wordpress Media Uploader not displaying image that has just been uploaded 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302936a1931710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论