【问题标题】:Can't upload image via submitting custom post from frontend in Wordpress无法通过在 Wordpress 中从前端提交自定义帖子来上传图片
【发布时间】:2020-07-21 18:35:18
【问题描述】:

我在从前端提交带有图片的自定义帖子时遇到问题。 我有一个包含帖子标题、描述和图片的表格,应该是帖子的特色图片。 因此,当用户提交帖子时,它实际上创建了标题和描述,但没有图像,即使在媒体库中也不会出现图像。 这是我的表格:

<form id="_themename-advert-create-form" method="post" enctype="multipart/form-data">
    <?php wp_nonce_field('_themename_submit_advert_action','__themename_submit_advert_nonce'); ?>
    <input type="hidden" name="_themename-advert-create-check" value="1" />
    
    <label for="_themename-advert-create-title">Title</label>
    <input id="_themename-advert-create-title" type="text" name="_themename-advert-create-title" />

    <label for="_themename-advert-create-content">Sample Content</label>
    <textarea id="_themename-advert-create-content" rows="8" name="_themename-advert-create-content"></textarea>

    <label for="_themename-advert-create-image">Upload Post Image</label>
    <input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />

    <input type="submit" id="_themename-advert-create-submit" value="SUBMIT" name="_themename-advert-create-submit" />

</form>

这是我创建自定义帖子的函数:

add_action('init', '_themename_create_new_post');
function _themename_create_new_post(){
    
    if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['_themename-advert-create-check']) && wp_verify_nonce( $_POST['__themename_submit_advert_nonce'], '_themename_submit_advert_action')) {
        $current_user = wp_get_current_user();
        $user_id = $current_user->ID;

        $new_post_title = sanitize_text_field($_POST['_themename-advert-create-title']);
        $new_post_content = sanitize_textarea_field($_POST['_themename-advert-create-content']);

        $new_post = array();
        $new_post['post_author'] = $user_id;
        $new_post['post_title'] = $new_post_title;
        $new_post['post_content'] = $new_post_content;
        $new_post['post_status'] = 'pending';
        $new_post['post_name'] = 'pending';
        $new_post['post_type'] = 'jana_usa';

        $post_success = wp_insert_post($new_post);
        
        if ( $_FILES ) { 
            $files = $_FILES["_themename-advert-create-image"];  
            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 ("_themename-advert-create-image" => $file);
                    foreach ($_FILES as $file => $array) {              
                        // $newupload = frontend_handle_attachment( $file, $post_success );
                        if ($_FILES[$file]['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, $post_success );

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

    }

}

提交后我收到错误Warning: Invalid argument supplied for foreach() in ... on line 43。这一行:

foreach ($files['name'] as $key => $value) { ...

我找不到解决方法。我做错了什么?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    问题出在这里:

    <input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
    

    应该是:

    <input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image[]" />
    

    仍然不明白为什么要在我的名字中添加 [],但现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2018-09-21
      • 2015-02-10
      • 2023-04-10
      • 2018-09-18
      • 2018-03-09
      • 2021-12-28
      • 1970-01-01
      相关资源
      最近更新 更多