J'essaie de sauvegarder les images soumises sur un formulaire frontal de manière à ce qu'elles soient accessibles via le backend d'ACF.
Dans comment télécharger une image sur ACF avec update_field sur wordpress , quelqu'un a fourni une solution pour un seul téléchargement d'image (et par conséquent, un seul champ d'image ACF). Je pensais qu'il devrait être assez facile d'étendre cela à un téléchargement d'images multiples (et à un champ de galerie ACF), mais j'obtiens une erreur que je ne peux pas localiser: Cannot redeclare wp_crop_image() in /../travel/wp-admin/includes/image.php on line 25
dans 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
);
}
}
}
sur mon formulaire.php:
<input type="file" name="upload_attachments[]">
<input type="file" name="upload_attachments[]">
dans 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);
}
J'apprécie votre aide et vos idées!
Argh..facile à réparer:
au lieu de
include( ABSPATH . 'wp-admin/includes/image.php' );
utilisation
require_once( ABSPATH . 'wp-admin/includes/image.php' );
J'espère que cela aide quelqu'un!