Support

Account

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

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