我正在尝试保存在前端表单上提交的图像,使其可以通过ACF后端访问。
在里面how to upload an image on ACF with update_field on wordpress, 有人提供了一个单一图像上传的解决方案(以及一个ACF图像字段)。我认为将其扩展到多图像上传(和ACF gallery字段)应该很容易,但我遇到了一个无法追踪的错误: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);
}
我感谢你的帮助和想法!