我正在使用 WordPress。我創建了一個自定義帖子型別。現在我必須添加檔案上傳選項,以便管理員可以上傳 pdf。
我已經嘗試了一些代碼,我得到了輸出并且它是正確的。

現在,我的問題是當我上傳 pdf 并單擊發布按鈕時,它沒有上傳。我沒有收到任何錯誤。我正在使用以下代碼。
function add_pdfcustom_meta_boxes() {
add_meta_box('wp_custom_attachment', 'Guideline Pdf Upload', 'wp_custom_attachment', 'guideline', 'normal', 'default');
}
add_action('add_meta_boxes', 'add_pdfcustom_meta_boxes');
function wp_custom_attachment() {
wp_nonce_field( basename( __FILE__ ), 'wp_custom_attachment_nonce');
$html = '<p >';
// $html .= 'Upload your PDF here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25">';
echo $html;
}
function save_pdfcustom_meta_data($id) {
/* --- security verification --- */
if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
return $id;
} // end if
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $id;
} // end if
if('page' == $_POST['post_type']) {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
} else {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
}
/* - end security verification - */
// Make sure the file array isn't empty
if(!empty($_FILES['wp_custom_attachment']['name'])) {
// Setup the array of supported file types. In this case, it's just PDF.
$supported_types = array('application/pdf');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['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, 'wp_custom_attachment', $upload);
update_post_meta($id, 'wp_custom_attachment', $upload);
} // end if/else
} else {
wp_die("The file type that you've uploaded is not a PDF.");
} // end if/else
} // end if
}
add_action('save_post', 'save_pdfcustom_meta_data');
function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
我正在嘗試從這個鏈接和這個鏈接
uj5u.com熱心網友回復:
要查看您的帖子是否有附件,請編輯以下功能
function wp_custom_attachment() {
wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
$file = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
if($file['url']):
$html .= 'Current file attached <a href="'.$file['url'].'" target="_blank">Preview file</a>';
endif;
$html .= '<p >';
$html .= 'Upload your PDF here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';
echo $html;
} // end wp_custom_attachment
我建議將 ACF 用于自定義欄位。沒有必要重新發明輪子。
請注意,這些檔案未在您的媒體庫中注冊,因此您必須手動從上傳檔案夾中洗掉檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/384567.html
標籤:WordPress的 pdf 邮政 自定义帖子类型
