Question

I'm using following Wordpress script to handle uploads from FineUploader:

require_once($_SERVER['DOCUMENT_ROOT']. '/wp-load.php');
include_once($_SERVER['DOCUMENT_ROOT']. '/wp-admin/includes/media.php');
include_once($_SERVER['DOCUMENT_ROOT']. '/wp-admin/includes/file.php');
include_once($_SERVER['DOCUMENT_ROOT']. '/wp-admin/includes/image.php');

if(!$_FILES) exit();

if(isset($_FILES['qqfile'])) {$files = $_FILES['qqfile'];}

$upload_dir = wp_upload_dir();

$file_name = strtolower(sanitize_file_name($files['name']));
$file_name = $upload_dir['path'] . '/' . basename($file_name);
$upload_overrides = array( 'test_form' => false );
$file_post = wp_handle_upload($files,$upload_overrides); //Posts File


$file_link = $file_post['file'];
$file_type = wp_check_filetype(basename($file_link), null); //File Extension

$post_name = preg_replace('/\.[^.]+$/', '', basename($file_link)); //Post Name

$attachment = array(
    'guid' => $file_link,
    'post_mime_type' => $file_type['type'],
    'post_title' => $post_name,
    'post_content' => '',
    'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment($attachment, $file_name, $_POST['post_id']);

$attach_data = wp_generate_attachment_metadata($attach_id, $file_name);

$attach_final = wp_update_attachment_metadata($attach_id, $attach_data);

$response['attachment_id'] = $attach_id;
$response['success'] = 'true';

echo json_encode($response);
exit();

I'm returning success, and attachment_id. How can I add the attachment ID to the generated preview, so I can pass it back when needing to delete the file?

Was it helpful?

Solution

The code below will take a parameter returned in the server's upload response and ensure it is passed as a parameter when a delete request is sent for the same file:

$("#fine-uploader-container").fineUploader({
    callbacks: {
        onComplete: function(id, name, response) {
            this.setDeleteFileParams({attachment_id: response.attachment_id}, id);
        }
    }
});

Note that the above code leaves out some required options (such as the request endpoint) in order to focus specifically on the problem at hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top