2013-03-26 8 views
10

Ho un modulo, che ha un campo di caricamento ($_FILES['watch_photo']).come caricare un'immagine su ACF con update_field su wordpress

Mi sono guardato intorno e sono venuto a mettere insieme questa funzione.

Prende sostanzialmente tutte le informazioni pertinenti in modo che sia riutilizzabile in futuro, restituirà un array di $ pid e l'URL del file, una volta terminato.

Il problema è che l'ACF non ha fornito molte informazioni su come aggiungere immagini alla sua campi 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']); 
    } 
    } 
} 

e poi ho usato il seguente codice:

  • per creare un post e quindi
  • caricare quindi utilizzare my_update_attachment per elaborare l'immagine
  • e infine aggiornare l'a dvanced campo personalizzato

Il codice:

$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); 

Che cosa mi manca? Qualsiasi aiuto sarebbe molto apprezzato.

+0

Come l'hai utilizzata la parte update_field()? era in una funzione? –

risposta

16

Ho avuto lo stesso problema, hai quasi avuto ragione, che mi ha aiutato anche.

guardò ACF e l'update_field accetta ID attaccamento come opporsi al la, pid o url

è possibile sostituire l'array di ritorno:

return array(
    'pid' =>$pid, 
    'url' =>$file['url'] 
); 

Con il seguente array:

return array(
    'pid' =>$pid, 
    'url' =>$file['url'], 
    'file'=>$file, 
    'attach_id'=>$attach_id 
); 

e quindi modificare

update_field('field_512e085a9372a',$att['url'],$watch); 

con il seguente

update_field('field_512e085a9372a',$att['attach_id'],$watch); 

ho anche bloggato su di esso qui http://www.jqui.net/wordpress/acf-upload-image-made-easy/

+0

salvato la mia giornata, bruh !! –

-1

Controllare questo plugin:

https://wordpress.org/plugins/acf-frontend-display/

  1. Crea modulo ACF con il campo frontendUploader (plug-in extention)
  2. Aggiungi la tua forma ACF in posta e con checkbox l'utilizzo del pannello di amministrazione postale per visualizzare sul fronte
+0

Il tuo plug-in non sembra funzionare con ACF V5. Quindi, non è davvero una soluzione. –