I follow the steps from here to integrate the plupload in metabox. Everything is working fine. But I did find one disadvantage of this guide. When I click the 'Remove' link from the uploaded images, it only removed it from the post, not actually deleting 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 clicked. 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?

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' );
        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.

  • 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, 2012 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. Nov 8, 2012 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, 2012 at 19:57

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.