I'm trying to save images submitted on a frontend form in a way that they are accessible via the ACF backend.
In how to upload an image on ACF with update_field on wordpress, someone provided a solution for a single image upload (and consequently a single ACF image field). I thought it should be pretty easy extending this to a multi image upload (and a ACF gallery field), but I get an error I can't track down:
Cannot redeclare wp_crop_image() in /../travel/wp-admin/includes/image.php on line 25
in functions.php
function RemapFilesArray($name, $type, $tmp_name, $error, $size)
{
return array(
'name' => $name,
'type' => $type,
'tmp_name' => $tmp_name,
'error' => $error,
'size' => $size,
);
}
function my_update_attachment($f,$pid,$t='',$c='') {
wp_update_attachment_metadata( $pid, $f );
if( !empty( $f['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
include( ABSPATH . 'wp-admin/includes/image.php' );
$override['test_form'] = false;
$file = wp_handle_upload( $f, $override );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($f['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $f['name'];
$type = $file['type'];
$title = $t ? $t : $name;
$content = $c;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $pid,
'post_mime_type' => $type,
'guid' => $file['url'],
);
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
if ( !is_wp_error( $attach_id )) {
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
return array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
}
}
}
on my form.php:
<input type="file" name="upload_attachments[]">
<input type="file" name="upload_attachments[]">
in process-form.php
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,$site_id);
array_push($gallery,$att['attach_id']);
}
update_field('field_1231242',$gallery,$site_id);
}
I appreciate your help and ideas!