Allow wp media library to check already selected images displayed in div

I would like to replicate the media selector library in my plugin: (I have a button, I click on this, the media library display, I select images and add in a div below) When I select one image or several images the button checked display for each image (in the right up corner). These selected images display well in my div but if I click again on the media library button my selected images are not checked.

I can't find the javascript documentation who explain how I get the selected image id allowing the library to define these images as selected. I tried to do that with the script in comment but it seems that the script media_selector_print_scripts doesn't exist anymore. the below script is located in a php file depending of main php file of plugin.

/*function media_selector_print_scripts() {
$my_saved_attachment_post_id = get_option( 'media_selector_attachment_id', 0 );
}
add_action( 'admin_footer', 'media_selector_print_scripts' );*/

var myplugin_media_upload;
//var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
//var set_to_post_id = ?php echo $my_saved_attachment_post_id; ?; // Set this

$('#btnFiles').click(function(e) {
        e.preventDefault();
        if( myplugin_media_upload ) {
            //myplugin_media_upload.uploader.uploader.param( 'post_id', set_to_post_id );
            myplugin_media_upload.open();
            return;
        }/*else {
            wp.media.model.settings.post.id = set_to_post_id;
        }*/
    
        myplugin_media_upload = wp.media.frames.file_frame = wp.media({
            title:Add a file,
            multiple: true
        });

        myplugin_media_upload.on( 'select', function(){
            var attachments = myplugin_media_upload.state().get('selection').map( 
                function( attachment ) {
                    attachment.toJSON();
                    return attachment;
            });

           var i;
           for (i = 0; i  attachments.length; ++i) {
                $('#block_file').preprend(
                    'img src=' + 
                    attachments[i].attributes.url + ' class=getFile'
                );
            }
        });
    myplugin_media_upload.open();

    });

Topic select media-library images Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.