ACFバックエンドからアクセスできるように、フロントエンドフォームで送信された画像を保存しようとしています。
wordpressでupdate_fieldを使用してACFに画像をアップロードする方法 で、誰かが単一の画像をアップロードするためのソリューションを提供しました(したがって単一のACF画像フィールド)。これをマルチイメージアップロード(およびACFギャラリーフィールド)に拡張するのはかなり簡単なはずだと思いましたが、追跡できないというエラーが表示されます。Cannot redeclare wp_crop_image() in /../travel/wp-admin/includes/image.php on line 25
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
);
}
}
}
私の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);
}
あなたの助けとアイデアに感謝します!
修正するのが簡単:
の代わりに
include( ABSPATH . 'wp-admin/includes/image.php' );
つかいます
require_once( ABSPATH . 'wp-admin/includes/image.php' );
これが誰かに役立つことを願っています!