Wordpress 上传前端表单不上传图片

Wordpress upload frontend form not uploading images

我正在使用 wordpress 的前端表单来创建自定义 post 类型并上传附加到该 post 的图片。

下面是我目前的代码。 post 已创建,但问题是图像未上传到附加到 post 的服务器。另一方面,我希望在发送表单之前对图像进行预可视化,就像 wordpress 在管理区域中的工作方式一样。

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['new_cpt_action'] ) &&  $_POST['new_cpt_action'] == "new_cpt" && isset( $_POST['new_cpt-form'] ) && wp_verify_nonce( $_POST['new_cpt-form'], 'create_new_cpt' ) ){

if (isset($_POST['submit'])) {
    $error = "";
    if (!empty($_POST['s_name'])) {
        $s_name =  $_POST['s_name'];
    } else {
        $error = 'Please insert a name';
    }
    if (empty($error)) {
        $new_post = array(
        'post_title'    =>  $s_name,
        'post_status'   =>  'pending', 
        'post_type' =>  'my_cpt',  
        );

        //Save the post
        $post_id = wp_insert_post($new_post);

        if( ! empty( $_FILES ) ) {
            foreach( $_FILES as $file ) {
                if( is_array( $file ) ) {
                    $attachment_id = wp_insert_attachment($file, $post_id);
                }
            }
        }

        //Redirect to the new post
        if ( $post_id ) {
                wp_redirect(home_url('site/'.$post_id));
            exit;
        }
    }
}

}

以及表格:

<form id="new_post" name="new_post" method="post" action="" class="" enctype="multipart/form-data">
    <input type="text" id="s_name" name="s_name"/>
    <input type="file" name="s_image" id="s_image" tabindex="25" />
    <input type="submit" value="Create post and upload image" id="submit" name="submit"/>
    <input type="hidden" id="new_cpt_action" name="new_cpt_action" value="new_cpt" />
</form>

如果你可以让我知道我遗漏了什么,那就太好了。 谢谢

我设法使用以下代码将附件包含在 wp 媒体库中:

if ( $_FILES ) { 
                $files = $_FILES["file"];  
                foreach ($files['name'] as $key => $value) {            
                        if ($files['name'][$key]) { 
                            $file = array( 
                                'name' => $files['name'][$key],
                                'type' => $files['type'][$key], 
                                'tmp_name' => $files['tmp_name'][$key], 
                                'error' => $files['error'][$key],
                                'size' => $files['size'][$key]
                            ); 
                            $_FILES = array ("file" => $file); 
                            foreach ($_FILES as $file => $array) {              
                                $newupload = frontend_handle_attachment( $file, $post_id ); 
                            }
                        } 
                    } 
                }

调用函数frontend_handle_attachment:

function frontend_handle_attachment($file_handler,$post_id) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

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

$attach_id = media_handle_upload( $file_handler, $post_id );

// Set featured image 
set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}

只需按如下形式输入即可正常工作:

<input type="file" name="file[]" multiple="multiple" />

关于进度条和图像的预可视化,我正在尝试实现 dropzone,但目前还没有成功。至少简单的方法有效。