admin管理员组

文章数量:1123933

I'm trying to figure out how it's possible to 'refresh' the media library in the new media dialogue in 3.5. I'm adding images from an external image library to the WordPress library (via a tab/iFrame combo in the media dialogue) which works ok, but I have to close and reopen the dialogue in order to show the newly added images (which have successfully been aded to the library).

I know there are wp.media.editor.open() and wp.media.editor.close() methods, but I can't quite manage to find a sort() or refresh() (or whatever) in the source. Truth be told, the source code is pretty intense reading and console.log doesn't seemingly help much with backbone.js. Any ideas?

I'm trying to figure out how it's possible to 'refresh' the media library in the new media dialogue in 3.5. I'm adding images from an external image library to the WordPress library (via a tab/iFrame combo in the media dialogue) which works ok, but I have to close and reopen the dialogue in order to show the newly added images (which have successfully been aded to the library).

I know there are wp.media.editor.open() and wp.media.editor.close() methods, but I can't quite manage to find a sort() or refresh() (or whatever) in the source. Truth be told, the source code is pretty intense reading and console.log doesn't seemingly help much with backbone.js. Any ideas?

Share Improve this question edited Feb 9, 2013 at 21:38 Chris_O 20.6k5 gold badges61 silver badges95 bronze badges asked Jan 4, 2013 at 15:34 Richard SweeneyRichard Sweeney 6441 gold badge6 silver badges16 bronze badges 3
  • 3 One more Question to the gallery "New Media Manager: The Great Unknown". By the rhythm it goes, I expect full documentation and hooks around WP 3.7... All the upvotes indicate a hungry community. – brasofilo Commented Feb 12, 2013 at 4:50
  • Instead of refresh() function , we can use combination of wp.media.editor.close() and wp.media.editor.open() function to achieve the desired output. – Vinod Dalvi Commented Feb 18, 2013 at 10:01
  • In the featured-image-picker for example, there is a switch in wp-includes/js/media-views.js line 3644 for whether or not media library has loaded. It would seem to be referencing wp.media.featuredImage.frame().views.get('.media-frame-content')[0].views.get("")[3].collection.length but setting that length to 0 doesn't do anything. – NoBugs Commented Oct 9, 2015 at 4:50
Add a comment  | 

4 Answers 4

Reset to default 7

The correct way of refreshing the content of the frame, as found in the WP core, is as below:

if(wp.media.frame.content.get()!==null){
   wp.media.frame.content.get().collection.props.set({ignore: (+ new Date())});
   wp.media.frame.content.get().options.selection.reset();
}else{
   wp.media.frame.library.props.set({ignore: (+ new Date())});
}

You should always check of the content is available, else refresh the library.

Cheers!

Took me a couple days but I finally dug my way through things enough to figure this 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!

2019 update. I found a better solution that doesnt break the uploader:

wp.media.frame.on('open', function() {
    if (wp.media.frame.content.get() !== null) {          
        // this forces a refresh of the content
        wp.media.frame.content.get().collection._requery(true);

        // optional: reset selection
        wp.media.frame.content.get().options.selection.reset();
    }
}, this);

is it what you are looking for

wp.media.editor.remove('content');
wp.media.editor.add('content');

本文标签: