Skip to content

Instantly share code, notes, and snippets.

@justlstn
Created August 24, 2018 10:18
Show Gist options
  • Save justlstn/26341a208261ef158e80e83fc4ed8cfd to your computer and use it in GitHub Desktop.
Save justlstn/26341a208261ef158e80e83fc4ed8cfd to your computer and use it in GitHub Desktop.
Add multiple files to acf repeater field
$files = $_FILES[ 'attachments' ];
$attachments = [];
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 ]
);
$attachments[] = $file;
}
}
foreach ($attachments as $file) {
if (is_uploaded_file($file['tmp_name'])) {
$remove_these = array(' ', '', '\"', '\\', '\/');
$newname = str_replace($remove_these, '', $file['name']);
$newname = time() . '-' . $newname;
$uploads = wp_upload_dir();
$upload_path = "{$uploads['path']}/$newname";
move_uploaded_file($file['tmp_name'], $upload_path);
$upload_file_url = "{$uploads['url']}/$newname";
$wp_filetype = wp_check_filetype(basename($upload_path), null );
$attachment = array(
'guid' => $upload_file_url,
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_path)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $upload_path, $post_id);
// Repeater child field name - "file"
$file_row = array(
'file' => $attach_id
);
// Repeater parent field name - "attachments"
add_row('attachments', $file_row, $post_id);
// require_once(ABSPATH . 'wp-admin/includes/image.php');
// $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_path );
// wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
@saidMounaim
Copy link

it's working ! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment