Media Uploader: get deleted files

I am currently working on a plugin to upload files to wordpress using the file uploader. The user can save a list of files to use in another location of the theme. Everything works, except when I want to delete files.

I can delete the files using the media uploader, which is no problem. The files are removed in the background of wordpress. But when a file is deleted in the media uploader, I also want it removed from the list the user is currently editing, so the files can’t be saved to my custom database table when it no longers exists in the “media environment”. I know which javascript I need for removing them from my list, but I don’t know how to get the deleted files from the media uploader.

Read More

Below are two possible solutions that would appear logical, but I haven’t found the correct approach just yet.

Approach 1

function someFunction() {
    (...)
    file_frame.on('select', handle_upload); //This works
    file_frame.on('delete', handle_delete_media); //perhaps something like this exists?
}

Approach 2 (Not sure if this is a good approach, as it requires selection of files)

function handle_upload() {
    var files = file_frame.state().get('selection').toJSON(); //This works
    var files = file_frame.state().get('deleted').toJSON(); //perhaps something like this exists?
    (...)
}

Any help would be greatly appreciated.

EDIT

This is the code for opening the Media Uploader

function open_uploader(e)
{
    e.preventDefault();

    if(file_frame) {
        file_frame.open();
        return;
    }

    file_frame = wp.media.frames.file_frame = wp.media({
        title: 'Select files for your List',
        multiple: true,
        button: {
            text: 'Insert into List',
        }
    });

    file_frame.on('select', handle_upload);
    //file_frame.on('delete', handle_media_delete); I NEED SOMETHING LIKE THIS

    file_frame.open();
}

And this code is the script to add the selected files to the list, which works already. This is basically just some jQuery-code.

function handle_upload(e)
{
    var files = file_frame.state().get('selection').toJSON();

    jQuery(files).each(function(index, attachment){
        var url = attachment.url.toString();
        var media_id = attachment.id.toString();
        var title = attachment.title.toString();
        var description = attachment.description.toString();
        (...)
    });
}

Related posts

1 comment

  1. Without seeing your “list editor” code I can only provide a theoretical answer.

    when a file is deleted in the media uploader, I also want it removed from the list the user is currently editing, so the files can’t be saved to my custom database table when it no longers exists in the “media environment”. I know which javascript I need for removing them from my list, but I don’t know how to get the deleted files from the media uploader.

    Before you save the list to your custom database table do a quick check if these media files exist in WordPress Media Library and filesystem.

    Exact solution depends on how you are saving to your custom database table. If you are using the WordPress Core APIs then this might help you: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data.

    For example:

    <?php
    function so111965_filter_deleted_handler( $data , $postarr ) {
      // do the filtering of deleted files here
      return $data;
    }
    
    add_filter( 'wp_insert_post_data', 'so111965_filter_deleted_handler', '99', 2 );
    ?> 
    

Comments are closed.