Support

Account

Home Forums Add-ons Repeater Field Multiple files add to acf repeater field from front end

Solving

Multiple files add to acf repeater field from front end

  • <form method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" enctype="multipart/form-data">
        <div class="form-group">
            <label>Invoices</label><br>
            <input type="file" class="form-input" name="invoice[]" multiple="">
            <small>Press Ctrl key to select multiple</small>
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="submit" class="form-submit" value="Add New Item" />
        </div>
    </form>

    I want to add images in to a new row in a repeater field.

  • I am assuming you got this worked out due to your later question.

  • Yes, i do that thing like this. But here only one repeated field pass to this function, is there any way to pass multiple fields and its values like an array. It’s a combination of image and input fields or textarea.

    function insert_repeater_row_attachment($files,$post_id,$mainrepeater,$repeaterfield)
    {
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        $count = 0;
        $galleryImages = array();
        foreach ($files['name'] as $count => $value)
        {
            if ($files['name'][$count])
            {
                $file = array(
                    'name'     => $files['name'][$count],
                    'type'     => $files['type'][$count],
                    'tmp_name' => $files['tmp_name'][$count],
                    'error'    => $files['error'][$count],
                    'size'     => $files['size'][$count]
                );
                $upload_overrides = array( 'test_form' => false );
                $upload = wp_handle_upload($file, $upload_overrides);
                $filename = $upload['file'];
                $parent_post_id = $post_id;
                $filetype = wp_check_filetype( basename( $filename ), null );
                $wp_upload_dir = wp_upload_dir();
                $attachment = array(
                    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                    'post_mime_type' => $filetype['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );
                $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
                $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
                wp_update_attachment_metadata( $attach_id, $attach_data );
            }
            // Repeater child field name - "file"
            $file_row = array( $repeaterfield => $attach_id );
            // Repeater parent field name - "attachments"
            add_row($mainrepeater, $file_row, $post_id);
        }
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.