comment transférer une image sur ACF avec update_field sur wordpress

J'ai un formulaire, qui a un chargement ( $_FILES['watch_photo'] ).

J'ai regardé autour et de mettre cette fonction.

Il prend toutes les informations pertinentes de manière à ce qu'il soit re-utilisable à l'avenir, il sera de retour un tableau de l' $pid, et l'URL du fichier, quand il est fait.

Le problème est que l'ACF n'a pas fourni beaucoup d'informations comment ajouter des images à ses champs à l'aide de update_field() http://www.advancedcustomfields.com/resources/functions/update_field/

function my_update_attachment($f,$pid,$t='',$c='') {
wp_update_attachment_metadata( $pid, $f );
if( !empty( $_FILES[$f]['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$file = wp_handle_upload( $_FILES[$f], $override );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($_FILES[$f]['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $file['filename'];
$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( $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']
);
//update_post_meta( $pid, 'a_image', $file['url'] );
}
}
}

et ensuite, j'ai utilisé le code suivant:

  • créer un post et puis
  • télécharger puis utilisez my_update_attachment pour traiter l'image
  • et enfin mise à jour de l'avancée du champ personnalisé

Le code:

$args= array(   'post_type' => 'watches',       'post_status' => 'publish'    );
$watch = wp_insert_post($args);
$att = my_update_attachment('watch_image',$watch);
update_field('field_512e085a9372a',$att['url'],$watch);

Ce qui me manque? toute aide serait grandement appréciée.

Comment avez-vous utilisé le update_field() de la partie? a été que dans une fonction?

OriginalL'auteur Lee Proctor | 2013-03-26