dongming4994 2017-09-25 15:59
浏览 67
已采纳

$ _FILES在WordPress上传元上传框中一直为空

I'm feeling silly. I'm building my first WordPress plugin. The first goal is to add a custom meta box to a post that will allows an audio upload. I have the meta box created, and when I use a text input, the code works fine. When I try to convert the input into a file upload though, I'm running into a problem.

  1. The file never uploads (I'm using wp_upload_bits())
  2. $_FILES seems to always be empty. In the below example, when I call something like $_FILES[ 'audio_box' ], I always get an invalid or unavailable key.

Here's a simplified version of the code I'm using. I have nonces and all that setup in the real version!

<?php

add_action( 'load-post.php', 'file_upload_setup' );
add_action( 'load-post-new.php', 'file_upload_setup' );

function file_upload_setup() {
    add_action( 'add_meta_boxes', 'file_upload_box' );
    add_action( 'save_post', 'upload_save_post_class_meta' );
}

function file_upload_box() {
    add_meta_box(
        'audio_box',    // Unique ID
        esc_html__( 'Title' ),
        'audio_box',   // Callback function
        'post',         // Admin page (or post type)
        'side',         // Context
        'default'         // Priority
    );
}

function upload_save_post_class_meta( $post_id ) {
    if ( !empty( $_FILES[ 'audio_box' ] ) ) {
        $upload = wp_upload_bits( $_FILES[ 'audio_box' ][ 'name' ],
            null,
            file_get_contents( $_FILES[ 'audio_box' ][ 'tmp_name' ] )
        ); 
    }
}

function file_upload_box( $post ) { ?>
    <p>
        <label for="file_upload_box"><?php _e( "Upload an audio file to accompany your post." ); ?></label>
        <br />
        <input
            class="widefat"
            type="file"
            name="audio_box" 
            id="audio_box" 
            value="<?php 
                    echo esc_attr(
                        get_post_meta( $post->ID, 'audio_box', true ) 
                    );
                ?>" 
            size="30" 
        />
    </p>
<?php }

?>

Is there anything special I need to do here to get the file to upload to the media library on save?

Thanks all! Long time listener, first time caller :)

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里