4

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.

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();
        (...)
    });
}
3
  • Would you mind adding that parts of your code that actually display/render and delete the files? Would be much easier to help you then.
    – kaiser
    Commented Aug 28, 2013 at 14:50
  • I'll edit my answer. Commented Aug 28, 2013 at 14:51
  • I've edited my answer. Commented Aug 28, 2013 at 14:58

1 Answer 1

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 );
?> 
1
  • Deleting the files using php is not the problem. What I want is some sort of javascript event fired from the media uploader whenever a file is deleted. I would then delete the file in the list (in the webbrowser, not background) using javascript, since it has been removed form the background :) It is just some "visual" removal from the list, that has nothing to do with the background :) Commented Aug 30, 2013 at 23:10

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.