TransWikia.com

Can't upload image via submitting custom post from frontend

WordPress Development Asked by AkForDev on October 30, 2021

I have an issue with submitting custom post from frontend with image. I have a form with post title, description and image, that should be featured image of the post. So when user submitting post, it actually creates with title and description, but without image, even in media library the image doesn’t appear. Here is my form:

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

Here is my function that creates custom post:

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

    }

}

After submittin I’m getting an error Warning: Invalid argument supplied for foreach() in ... on line 43. This line:

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

I can’t find how to solve it. What am I doing wrong?

One Answer

The problem was here:

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

It should be:

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

Still can't understand why I should add [] in my name, but now everything is working.

Answered by AkForDev on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP