How to upload multiple images on frontend to ACF gallery using update_field? Not working

I have a same situation as in this link How to upload multiple images on frontend to ACF gallery using update_field.

but the code is not working for me: it does not add any images to acf image gallery.

My code :

process-form.php

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) { 
    $title =  $_POST['title'];

} else { 
    echo 'Please enter a title';
}
if (isset ($_POST['description'])) { 
    $description = $_POST['description']; 
} else { 
    echo 'Please enter the content'; 
}

// Add the content of the form to $post as an array
$post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_category' => array($_POST['cat']),

    'post_status'   => 'publish',         // Choose: publish, preview, future, etc.
    'post_type'     => 'work'  // Use a custom post type if you want to
);
$pid = wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function 



if ($_FILES) {

    print_r($_FILES);
    array_reverse($_FILES);
    $i = 0;//this will count the posts
    foreach ($_FILES as $file => $array) {
        if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
        else $set_feature = 0; //if $i!=0 we are not dealing with the first post
        $newupload = insert_attachment($file,$pid, $set_feature);
        echo $i++; //count posts
    }

    if ($_FILES['upload_attachment']){

        $files = array_map('RemapFilesArray',
        $_FILES['upload_attachment']['name'],
        $_FILES['upload_attachment']['type'],
        $_FILES['upload_attachment']['tmp_name'],
        $_FILES['upload_attachment']['error'],
        $_FILES['upload_attachment']['size']
        );

        $gallery = array();

        foreach ($files as $file)
        { //loop through each file
            $att = my_update_attachment($file,$pid);
            array_push($gallery,$att['attach_id']);
        }
        update_field('field_58f67656b55f2',$gallery,$pid);

    }


} 


$category_filter = array($_POST['cat_filter']);

}

functions.php is same as above except that my post type here is work.

and my form is :

<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
    <label for="title">Title :</label><br />
    <input class="form-control" type="text" id="title" value="" tabindex="1" size="20" name="title" minlength="5" required />
</div>
<div class="form-group">
    <label for="description">Description :</label><br />
    <textarea class="form-control" id="description" tabindex="3" name="description" cols="50" rows="6" minlength="30" required></textarea>
</div>
<div class="form-group">
    <?php 
    $args_drop = array(
        'show_option_none'   => 'Category',
        'hide_empty'         => false,
        'include'            => '26, 27, 29, 34, 33',
        'tab_index'          => 3,
        'taxonomy'           => 'category',
    );
    wp_dropdown_categories($args_drop);
    ?>
</div>
<div class="form-group">
    <label for="file">Image :</label>
    <input type="file" name="file" id="file" required><br>
    <input type="file" name="upload_attachments[]">
    <input type="file" name="upload_attachments[]">
</div>  


<input type="submit" value="Submit" tabindex="6" id="submit" name="submit" />

<input type="hidden" name="post_type" id="post_type" value="domande" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>

Read more here: How to upload multiple images on frontend to ACF gallery using update_field? Not working

Leave a Reply

Your email address will not be published. Required fields are marked *