我正在为客户端开发一个插件,他们需要能够将几个PDF作为元数据附加到帖子。客户端需要偶尔更新这些附件,但不删除帖子本身。我有上传/附件工作。
我已经设置了一个自定义的帖子类型来处理插件中的所有内容。
我已经创建了两个上载元框(因为我不知道一个上载元框支持多个文件),我的上载工作正常,文件作为元数据正确写入数据库,并保存了已使用的nonce。
我需要为用户添加删除当前附件的功能,并使用我的简单上传程序替换它们。这就是我迷路的地方。我真的不知道如何使用delete\\u post\\u meta,也许?不管怎样,这是代码。非常感谢您的帮助!!!
/**
*Uploader 1
*
*
*/
function profoal_pdf_upload() {
add_meta_box(\'enicks_report\', \'Enicks Report\', \'enicks_report\', \'broodmare\', \'normal\', \'high\');
}
add_action(\'add_meta_boxes\', \'profoal_pdf_upload\');
function enicks_report() {
wp_nonce_field(plugin_basename(__FILE__), \'enicks_report_nonce\');
$html = \'<p class="description">\';
$html .= \'Upload eNicks here\';
$html .= \'</p>\';
$html .= \'<input type="file" id="enicks_report" name="enicks_report" value="" size="25">\';
echo $html;
}
add_action(\'save_post\', \'save_custom_meta_data\');
function save_custom_meta_data($id) {
if(!empty($_FILES[\'enicks_report\'][\'name\'])) {
$supported_types = array(\'application/pdf\');
$arr_file_type = wp_check_filetype(basename($_FILES[\'enicks_report\'][\'name\']));
$uploaded_type = $arr_file_type[\'type\'];
if(in_array($uploaded_type, $supported_types)) {
$upload = wp_upload_bits($_FILES[\'enicks_report\'][\'name\'], null, file_get_contents($_FILES[\'enicks_report\'][\'tmp_name\']));
if(isset($upload[\'error\']) && $upload[\'error\'] != 0) {
wp_die(\'There was an error uploading your file. The error is: \' . $upload[\'error\']);
} else {
add_post_meta($id, \'enicks_report\', $upload);
update_post_meta($id, \'enicks_report\', $upload);
}
}
else {
wp_die("The file type that you\'ve uploaded is not a PDF.");
}
}
}
/**
* Uploader 2
*
*/
function profoal_pedigree_upload() {
add_meta_box(\'pedigree\', \'Pedigree\', \'pedigree\', \'broodmare\', \'normal\', \'high\');
}
add_action(\'add_meta_boxes\', \'profoal_pedigree_upload\');
function pedigree() {
wp_nonce_field(plugin_basename(__FILE__), \'pedigree_nonce\');
$html = \'<p class="description">\';
$html .= \'Upload pedigree here\';
$html .= \'</p>\';
$html .= \'<input type="file" id="pedigree" name="pedigree" value="" size="25">\';
echo $html;
}
add_action(\'save_post\', \'save_custom_pedigree_upload\');
function save_custom_pedigree_upload($id) {
if(!empty($_FILES[\'pedigree\'][\'name\'])) {
$supported_types = array(\'application/pdf\');
$arr_file_type = wp_check_filetype(basename($_FILES[\'pedigree\'][\'name\']));
$uploaded_type = $arr_file_type[\'type\'];
if(in_array($uploaded_type, $supported_types)) {
$upload = wp_upload_bits($_FILES[\'pedigree\'][\'name\'], null, file_get_contents($_FILES[\'pedigree\'][\'tmp_name\']));
if(isset($upload[\'error\']) && $upload[\'error\'] != 0) {
wp_die(\'There was an error uploading your file. The error is: \' . $upload[\'error\']);
} else {
add_post_meta($id, \'pedigree\', $upload);
update_post_meta($id, \'pedigree\', $upload);
}
}
else {
wp_die("The file type that you\'ve uploaded is not a PDF.");
}
}
}
function update_edit_form() {
echo \' enctype="multipart/form-data"\';
}
add_action(\'post_edit_form_tag\', \'update_edit_form\');
/**
* Display the attached pedigree & eNicks report
*
*/
function profoal_pdf_display() {
add_meta_box(\'display_pdf_attachments\', \'Attached Reports\', \'display_pdf_attachments\', \'broodmare\', \'normal\', \'high\');
}
add_action(\'add_meta_boxes\', \'profoal_pdf_display\');
function display_pdf_attachments () {
global $post;
wp_nonce_field(plugin_basename(__FILE__), \'display_pdf_attachments_nonce\');
$pdf = get_post_meta($post->ID, \'enicks_report\', true );
$pdf2 = get_post_meta($post->ID, \'pedigree\', true);
echo \'<p><b> Current eNicks Report:</b>\' . $pdf[\'url\'] . \'</p>\';
echo \'<p><b>Current pedigree:</b>\' . $pdf2[\'url\'] . \'</p>\';
echo \'<p> If you want to update a report, delete it, attached the new one above, and update the post.</p>\';
}