dongyu1983 2016-07-29 06:34
浏览 37

自定义帖子表单中的wordpress附件错误

I have a custom form for editing custom post and this form is for frontend users to edit their posts and respective attachments. For the attachments part I have two input file elements in the form where an image and a video can be uploaded and I arrived at below PHP code to capture the uploaded attachments.

            if ($_FILES) {

        foreach ($_FILES as $file => $array) {

            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK || $_FILES[$file]['error'] === UPLOAD_ERR_NO_FILE) {
                $ermg = "upload error : " . $_FILES[$file]['error'];
                continue;
            } else {
                $attach_id = media_handle_upload($file, $post_id);
                $type = get_post_mime_type($attach_id);
                if ($type === 'image/jpeg' || $type === 'image/png') {
                    update_post_meta($new_post, '_thumbnail_id', $attach_id);
                } elseif ($type === 'video/mp4' || $type === 'video/quickime') {
                    update_post_meta($new_post, '_video_id', $attach_id);
                }
            }
            return $ermg;
        }
    }

Here the problem is my code is not capturing only one attachment if both are uploaded at same time, where as uploading a single attachment works fine.

Along with this there is one more issue, I am seeing a new unknown attachment with no title being formed in the media library every time some upload happens, below is the screenshot and this new unknown attachment is not expected. Can anyone please help? enter image description here

  • 写回答

1条回答 默认 最新

  • duanbinian2243 2016-07-29 07:19
    关注

    Try this : (in function.php)

    require_once(ABSPATH.'wp-admin/includes/admin.php');
    if ($_FILES) 
    {
        foreach ($_FILES as $file => $array) 
        {    
            $file_return = wp_handle_upload( $file, array('test_form' => false ) );
            if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) 
            {
                $ermg = "upload error : " . $_FILES[$file]['error'];
                continue;
            } 
            else 
            {
                $filename=$file_return['file'];
                $type=$file_return['type'];
                $attachment=array(
                    'post_mime_type' => $file_return['type'],
                    'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'guid' => $file_return['url']
                );
                $attachment_id=wp_insert_attachment($attachment, $file_return['url']);
                require_once(ABSPATH . 'wp-admin/includes/image.php');
                $attachment_data=wp_generate_attachment_metadata( $attachment_id, $filename );
                wp_update_attachment_metadata($attachment_id,$attachment_data);
                if ($type === 'image/jpeg' || $type === 'image/png') {
                    update_post_meta($new_post, '_thumbnail_id', $attachment_id);
                } elseif ($type === 'video/mp4' || $type === 'video/quickime') {
                    update_post_meta($new_post, '_video_id', $attachment_id);
                }   
            }
            return $ermg;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端