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 follow the steps from here to integrate the plupload in metabox. Everything is working fine. But I do found one disadvantages of this guide. When I click 'Remove' link from the uploaded images, it only removed it from the post, not actually delete it in the directory.

Another example of using plupload in metabox is RW Metabox plugin. This plugin does delete the file entirely when the "Delete" link is click. But I just can't figure it how he did in their code.

Can somebody here tell me how can I apply the 'Delete' feature in plupload with jquery?

share|improve this question

1 Answer 1

To delete a file from the file system, you can use something like this attached to an AJAX hook that is called by jQuery:

function ajax_remove_image() {
    check_ajax_referer( 'image_removal', 'image_removal_nonce' );

    if ( ! $_POST[ 'confirm' ] )
        exit( 'false' );

    $uploads       = wp_upload_dir();
    $upload_dir    = $uploads[ 'path' ];
    $file          = strpos( $_POST[ 'file' ], '/' ) !== false ? $_POST[ 'file' ] : $upload_dir . '/' . $_POST[ 'file' ];
    $attachment_id = null;

    @unlink( $file );

    if ( ! file_exists( $file ) )
        exit( 'true' );
    else
        exit( 'false' );
}

This code comes from a plug-in that I am currently working on (source), so you may have to adjust it as necessary.

share|improve this answer
    
Hi, thanks for the tips. I will study your code more deeply.By the way, What is the data in your $_POST[ 'file' ] ? How about if my $_POST[ 'file' ] is the url of the image (not the path of the image)? Is it still applicable by above code? –  dev-jim Nov 8 '12 at 15:44
    
I'm not sure how you would it with a URL. You probably need to convert it to a path using the dirname and/or basename functions. –  Joseph Nov 8 '12 at 17:20
    
It is because the way I write the jquery giving me back the url into the remove field instead of image name (like yours). I try to tweak it to see how it work..anyway thanks for your tips. –  dev-jim Nov 8 '12 at 19:57

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.