25

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?

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. 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

4 Answers 4

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!

2
  • Where is this in WP core?
    – NoBugs
    Commented Oct 8, 2015 at 16:04
  • Are you sure this is still correct? It seems wp.media.featuredImage.frame().options.selection.reset exists but there is no wp.media.featuredImage.frame().collection for example.
    – NoBugs
    Commented Oct 9, 2015 at 3:36
4

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!

6
  • 1
    seems so intuitive :) nevertheless, this works!! THANK YOU!!!
    – Andrej
    Commented Jun 24, 2013 at 18:58
  • 3
    just found a "more official" 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. Commented Apr 21, 2014 at 17:31
  • 2
    While both commands do trigger a refresh, afterwards you can no longer add images in the modal :( need another solution to this Commented Jun 21, 2014 at 13:22
  • This is still a far from ideal solution, but it's worth noting the media-frame-content key has changed from 1 to 2: wp.media.editor.get(wpActiveEditor).views._views[".media-frame-content"][0].views._views[""][2].collection.props.set({ignore:(+(new Date()))})
    – Ian
    Commented Sep 23, 2016 at 12:35
  • For the above reason you're better off using the more generic version posted elsewhere in this question: wp.media.frame.content.get().collection.props.set({ignore: (+ new Date())});
    – Ian
    Commented Sep 23, 2016 at 12:52
2

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);
-1

is it what you are looking for

wp.media.editor.remove('content');
wp.media.editor.add('content');
1
  • 1
    Please explain why that could solve the problem.
    – fuxia
    Commented Feb 26, 2013 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.