【问题标题】:How to add dynamic multiple file attached for custom post type meta box in wordpress如何在wordpress中为自定义帖子类型元框添加动态多个文件
【发布时间】:2018-09-30 06:35:54
【问题描述】:

我正在尝试为我的 WordPress 网站创建一个名为 books 的自定义帖子类型。我当前的代码可以从元框上传一个文件。有什么方法可以动态上传多个文件或根据元框自定义帖子类型的条件上传?

我当前的代码

<?php
function add_custom_meta_boxes() {

    // Define the custom attachment for posts
    add_meta_box(
        'wp_custom_attachment',
        'Custom Attachment',
        'wp_custom_attachment',
        'books', //post type name
        'side'
    );

} // end add_custom_meta_boxes
add_action('add_meta_boxes', 'add_custom_meta_boxes');

function wp_custom_attachment() {

    wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');

    $html = '<p class="description">';
    $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


function save_custom_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('books' == $_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 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

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    上传多个文件需要您以不同的方式使用$_FILES,因为 PHP 是如何组织上传数据的。请参阅this documentation 了解更多信息。

    您的代码中的另一个问题是它不会为上传的文件生成元数据,这使得它们在媒体库中不可用。

    为避免这些问题,我建议使用custom fields plugin 自动为您处理这些问题。

    【讨论】:

    • 确实为多个输入文件使用原始 php 文档会成功,尽管我认为没有必要为上传的文件使用元数据,以防您只需要上传文件而不是图像。只需遵循 php 文档并调整当前代码即可。无需复杂化。
    猜你喜欢
    • 2012-12-03
    • 2019-11-26
    • 2014-08-09
    • 2011-11-19
    • 2014-11-18
    • 2018-02-23
    • 2016-05-13
    • 2021-05-22
    • 2014-11-02
    相关资源
    最近更新 更多