Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
        (...)
    });
}
share|improve this question
    
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 Aug 28 '13 at 14:50
    
I'll edit my answer. –  Michiel Standaert Aug 28 '13 at 14:51
    
I've edited my answer. –  Michiel Standaert Aug 28 '13 at 14:58

1 Answer 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 );
?> 
share|improve this answer
    
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 :) –  Michiel Standaert Aug 30 '13 at 23:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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