我正在使用以下Wordpress脚本来处理来自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();
我正在返回success
和attachment_id
。如何将附件ID添加到生成的预览中,以便在需要删除文件时将其传回?