Pre-Select Images when opening WordPress 3.5 media manager Pre-Select Images when opening WordPress 3.5 media manager wordpress wordpress

Pre-Select Images when opening WordPress 3.5 media manager


After studying the core for a bit, the answer here is really quite straightforward.

Listen for the open event of the wp.media object, grab the state, create attachment objects with your id's and add them to the selection.

frame.on('open',function() {  var selection = frame.state().get('selection');  var ids_value = jQuery('#my_field_id').val();  if(ids_value.length > 0) {    var ids = ids_value.split(',');    ids.forEach(function(id) {      attachment = wp.media.attachment(id);      attachment.fetch();      selection.add(attachment ? [attachment] : []);    });  }});

This works when selecting multiple images as well as single and assumes that using the code above you have stored the values in a single text/hidden field with comma separation.


not a real answer, but somethings that I have noticed

using your code the frame.open( console.log('open') ) does trigger the console.log.
The other frame.on('open', function() { console.log('on->open')}) does not.

When looking at the post edit page. (Where a featured image is already set).If you open the featured img window a few things happen that are interesting.

  1. WP does 3 ajax calls, the 1st and 3rst contain the featured img id. the 2nd is the same as with your code.

  2. when the popup is loaded the featured image is visible / loaded before the rest of the images. When those show up the featured image is put in the right order.

  3. When looking in firebug at the dom tab I discovered that the var wp.media.model.settings.post.featuredImageId holds (wait for it) the featured image value.

Hopes this helps you in some way.


I think those guy manage to do it : https://wordpress.stackexchange.com/questions/76125/change-the-default-view-of-media-library-in-3-5/76213#76213But this doesn't work for me.I ve got the jquery in the footer of my post/edit, post/new but that just don't work for me :(