Created
August 24, 2018 10:18
-
-
Save justlstn/26341a208261ef158e80e83fc4ed8cfd to your computer and use it in GitHub Desktop.
Add multiple files to acf repeater field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 ); | |
} | |
} |
What does this script do again?
I've used this script once for uploading files to the wp-uploads
directory but rename them before and then attach each file to its own custom field repeater. Not sure it's right enough.
it's working ! thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does this script do again?