上载不适用于自定义插件开发

时间:2019-01-03 10:06:07

标签: php wordpress

我正在开发一个插件,该插件允许添加新的视频信息,在该插件中还有其他字段,尤其是用于上传特色图片的字段。

我尝试了几种功能,例如media_handle_upload或wp_handle_upload,但是没有一个主题可以工作。不管max_upload_size等如何,其他所有配置都是正确的。另外,我尝试通过直接在wp-admin中添加媒体来进行上传,并且效果很好。

我不知道为什么会这样。我收到的错误是:

> bject(WP_Error)#1844 (2) { ["errors"]=> array(1) { ["upload_error"]=> array(1) { [0]=> string(212) "File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini." } } ["error_data"]=> array(0) { } }

我正在工作的代码如下:

 require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'video_photo_url', 0 );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.

        var_dump($attachment_id);
        die();
    } else {
        // The image was uploaded successfully!
        var_dump($attachment_id);
        die();
    } 

形式如下:

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
        <table class='wp-list-table widefat fixed'>


            <tr>
                <th class="ss-th-width">Upload Photo</th>
                <td><input type="file" name="video_photo_url" id="fileToUpload"></td>
            </tr>

            <tr>
                <th class="ss-th-width">Video URL</th>
                <td><input type="text" name="video_url" value="<?php echo $name; ?>" class="ss-field-width" style="width:100%;" /></td>
            </tr>
        </table>
        <input type='submit' name="insert" value='Save' class='button'>
    </form>

希望有人可以帮助我解决这个问题。非常感谢你!

1 个答案:

答案 0 :(得分:0)

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">

尝试提及enctype。它告诉表单使用表单发布文件。我想这是您的问题

相关问题