admin管理员组文章数量:1124161
I'm developing a plugin that adds a new tab to the media modal, and I need to know a way trigger a refresh of the attachments tab so it shows newly added attachments. This is the code I'm using:
wp.media.view.Toolbar.Custom = wp.media.view.Toolbar.extend({
initialize: function() {
_.defaults( this.options, {
event: 'custom_event',
close: false,
items: {
custom_event: {
text: wp.media.view.l10n.customButton,
style: 'primary',
priority: 80,
requires: false,
click: this.addAttachment
}
}
});
wp.media.view.Toolbar.prototype.initialize.apply( this, arguments );
},
// triggered when the button is clicked
addAttachment: function(){
this.controller.state().addAttachment();
this.controller.setState( 'insert' );
// I NEED TO TRIGGER A REFRESH OF THE ATTACHMENTS TAB HERE
}
});
Any help would be appreciated. The media modal documentation is almost non-existant.
Thanks
I'm developing a plugin that adds a new tab to the media modal, and I need to know a way trigger a refresh of the attachments tab so it shows newly added attachments. This is the code I'm using:
wp.media.view.Toolbar.Custom = wp.media.view.Toolbar.extend({
initialize: function() {
_.defaults( this.options, {
event: 'custom_event',
close: false,
items: {
custom_event: {
text: wp.media.view.l10n.customButton,
style: 'primary',
priority: 80,
requires: false,
click: this.addAttachment
}
}
});
wp.media.view.Toolbar.prototype.initialize.apply( this, arguments );
},
// triggered when the button is clicked
addAttachment: function(){
this.controller.state().addAttachment();
this.controller.setState( 'insert' );
// I NEED TO TRIGGER A REFRESH OF THE ATTACHMENTS TAB HERE
}
});
Any help would be appreciated. The media modal documentation is almost non-existant.
Thanks
Share Improve this question edited May 27, 2019 at 12:22 tru.d 1861 gold badge1 silver badge17 bronze badges asked Feb 25, 2015 at 8:33 leemonleemon 2,0124 gold badges22 silver badges51 bronze badges 4 |3 Answers
Reset to default 2You can checkout this link https://codex.wordpress.org/Javascript_Reference/wp.media
jQuery(function($){
// Set all variables to be used in scope
var frame,
metaBox = $('#meta-box-id.postbox'), // Your meta box id here
addImgLink = metaBox.find('.upload-custom-img'),
delImgLink = metaBox.find( '.delete-custom-img'),
imgContainer = metaBox.find( '.custom-img-container'),
imgIdInput = metaBox.find( '.custom-img-id' );
// ADD IMAGE LINK
addImgLink.on( 'click', function( event ){
event.preventDefault();
// If the media frame already exists, reopen it.
if ( frame ) {
frame.open();
return;
}
// Create a new media frame
frame = wp.media({
title: 'Select or Upload Media Of Your Chosen Persuasion',
button: {
text: 'Use this media'
},
multiple: false // Set to true to allow multiple files to be selected
});
// When an image is selected in the media frame...
frame.on( 'select', function() {
// Get media attachment details from the frame state
var attachment = frame.state().get('selection').first().toJSON();
// Send the attachment URL to our custom image input field.
imgContainer.append( '<img src="'+attachment.url+'" alt="" style="max-width:100%;"/>' );
// Send the attachment id to our hidden input
imgIdInput.val( attachment.id );
// Hide the add image link
addImgLink.addClass( 'hidden' );
// Unhide the remove image link
delImgLink.removeClass( 'hidden' );
});
// Finally, open the modal on click
frame.open();
});
// DELETE IMAGE LINK
delImgLink.on( 'click', function( event ){
event.preventDefault();
// Clear out the preview image
imgContainer.html( '' );
// Un-hide the add image link
addImgLink.removeClass( 'hidden' );
// Hide the delete image link
delImgLink.addClass( 'hidden' );
// Delete the image id from the hidden input
imgIdInput.val( '' );
});
});
Try out:
wp.media.editor.get(wpActiveEditor).views._views[".media-frame-content"][0].views._views[""][1].collection.props.set({ignore:(+(new Date()))})
Seems like there must be an easier way but that works for me in the meantime!
A more better way to do it:
wp.media.frame.content.get('gallery').collection.props.set({ignore: (+ new Date())});,
in this case i'm refreshing the gallery tab.
Try both of the above codes and see which one works best for you.
This works in the latest version of WordPress which is now 6.5:
wp.media.frame.content.get().collection._requery(true);
wp.media.frame.content.get().options.selection.reset();
本文标签:
版权声明:本文标题:attachments - How to trigger a refresh in the media modal 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736600971a1945217.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this.controller.state().addAttachment()
function is just an AJAX call usingwp.media.post()
, so I'd need to trigger an hypothetical "model updated" event somewhere after this AJAX call. Any ideas? – leemon Commented Feb 25, 2015 at 10:49