WordPress的上传前端形式不上传图像 [英] Wordpress upload frontend form not uploading images

查看:183
本文介绍了WordPress的上传前端形式不上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我的代码如下所示。该帖子已创建,但问题是图像未上传到附加到该帖子的服务器上。另一方面,我希望在发送表单之前将图像预先可视化,就像wordpress为管理区域一样。


$ b $ pre $ 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 ='请插入一个名字';
$ post_title'=> $ s_name,
'post_status'=>
$ b $ 'pending',
'post_type'=>'my_cpt',
);

//保存帖子
$ post_id = wp_insert_post($ new_post);
$ b $ if(!empty($ _FILES)){
foreach($ _FILES as $ file){
if(is_array($ file)){
$ attachment_id = wp_insert_attachment($ file,$ post_id);



$ b //重定向到新的
if($ post_id){
wp_redirect(home_url('site /'.$post_id));
出口;



$ / code $ / pre
$ b

}



形式:

 < form id =new_postname =new_post method =postaction =class =enctype =multipart / form-data> 
< input type =textid =s_namename =s_name/>
< input type =filename =s_imageid =s_imagetabindex =25/>
< input type =submitvalue =创建帖子并上传图片id =submitname =submit/>
< input type =hiddenid =new_cpt_actionname =new_cpt_actionvalue =new_cpt/>
< / form>

如果你能让我知道我错过了什么会很棒。
谢谢

解决方案

我使用下面的代码将附件包含在wp媒体库中: b
$ b

  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'=> $档案['size'] [$ key]
);
$ _FILES = array(file=> $ file);
foreach($ _FILES as $ file => $ array){
$ newupload = frontend_handle_attachment($ file,$ post_id);




code $


调用函数frontend_handle_attachment:
$ b $ pre $函数frontend_handle_attachment($ file_handler,$ post_id){
//检查以确保它的($ _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_post_thumbnail($ post_id,$ attach_id);
返回$ attach_id;

}



简单的输入形式如下:

 < input type =filename =file []multiple =多个/> 

至于图像的进度条和预先可视化,我试图实现dropzone,但到目前为止还没有运气。至少这个简单的方法是有效的。


I'm working on a frontend form in wordpress to create a custom post type and upload a picture attached to that post.

Below is my code so far. The post is created, but problem is that the images are not uploaded to the server either attached to that post. On the other hand I would like the images to be pre-visualized before sending the form, in the same way wordpress works for the admin area.

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;
        }
    }
}

}

And the form:

<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>

If you could please let me know what I'm missing would be great. Thanks

解决方案

I managed to include the attachments in the wp media library with this code:

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 ); 
                            }
                        } 
                    } 
                }

Which calls the function 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;

}

This works fine with a simple input in the form as follows:

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

As for the progress bar and pre-visualization of the images, I'm trying to implement dropzone, but no luck so far. At least the simple approach works.

这篇关于WordPress的上传前端形式不上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆