Lee Proctor
Tengo un formulario, que tiene una carga ($_FILES['watch_photo']
) campo.
Miré a mi alrededor y llegué a armar esta función.
Básicamente, toma toda la información relevante para que sea reutilizable en el futuro, devolverá una matriz de $pid y la URL del archivo, cuando haya terminado.
El problema es que ACF no ha proporcionado mucha información sobre cómo agregar imágenes a sus campos usando 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'] );
}
}
}
y luego usé el siguiente código:
- para crear una publicación y luego
- cargar y luego usar
my_update_attachment
para procesar la imagen - y finalmente actualice el campo personalizado avanzado
El código:
$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);
¿Qué me estoy perdiendo? cualquier ayuda sería muy apreciada.
valle
Tuve el mismo problema, casi lo hiciste bien, lo que también me ayudó.
Miró ACF y el update_field
acepta la identificación del archivo adjunto en lugar de la, pid
o url
Puede reemplazar la matriz de retorno:
return array(
'pid' =>$pid,
'url' =>$file['url']
);
Con la siguiente matriz:
return array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
y luego cambiar
update_field('field_512e085a9372a',$att['url'],$watch);
con lo siguiente
update_field('field_512e085a9372a',$att['attach_id'],$watch);
-
ese enlace del blog es spam, no hagas clic
– Nate Beers
30 oct 2019 a las 18:27
-
@NateBeers Saludos, he eliminado el enlace
– Vale
31 oct 2019 a las 10:25
debería esta línea
if ( !is_wp_error( $id )) {
ser
if ( !is_wp_error( $attach_id )) {
Compruebe este complemento:
https://wordpress.org/plugins/acf-frontend-display/
- Crear formulario ACF con campo frontendUploader (extensión de complemento)
- Agregue su formulario ACF en la publicación y con el panel de administración de la publicación use la casilla de verificación para ver en el frente
-
Su complemento no parece funcionar con ACF V5. Entonces, no es realmente una solución.
-Miguel Tavares
26 de abril de 2015 a las 16:52
¿Cómo usaste la parte update_field()? ¿Eso fue en una función?
– Andrés Lázaro
25 de marzo de 2015 a las 15:57