Advertisement
ziobudda

Untitled

Apr 5th, 2020
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. // $fields['immagine']['raw_value']; => /var/www/miosito/web_dir/wp-contents/upload/mia_img.gif
  2. // $post_id è il post a cui collegare questa immagine tramite ACF
  3.  
  4. $file = $fields['immagine']['raw_value'];
  5.         $filename = basename($file);
  6.  
  7.         $upload_file = wp_upload_bits($filename, NULL, file_get_contents($file));
  8.         if (!$upload_file['error']) {
  9.           $wp_filetype = wp_check_filetype($filename, NULL);
  10.           $attachment = [
  11.             'post_mime_type' => $wp_filetype['type'],
  12.             'post_parent' => $post_id,
  13.             'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
  14.             'post_content' => '',
  15.             'post_status' => 'inherit'
  16.           ];
  17.           $attachment_id = wp_insert_attachment($attachment, $fields['immagine']['raw_value'], $post_id);
  18.           if (!is_wp_error($attachment_id)) {
  19.             require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  20.             $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
  21.             wp_update_attachment_metadata($attachment_id, $attachment_data);
  22.             zbl_log($attachment_id, FALSE, "file caricato");
  23.             update_field('servizio_immagine', $attachment_id, $post_id);
  24.           }
  25.           else {
  26.             zbl_log($attachment_id->get_error_message(), FALSE, "Errore in wp_insert_attach");
  27.           }
  28.         }
  29.         else {
  30.           zbl_log($upload_file['error'], FALSE, "Errore in upload_file");
  31.         }
  32.  
  33.         wp_update_post($post_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement