Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // $fields['immagine']['raw_value']; => /var/www/miosito/web_dir/wp-contents/upload/mia_img.gif
- // $post_id è il post a cui collegare questa immagine tramite ACF
- $file = $fields['immagine']['raw_value'];
- $filename = basename($file);
- $upload_file = wp_upload_bits($filename, NULL, file_get_contents($file));
- if (!$upload_file['error']) {
- $wp_filetype = wp_check_filetype($filename, NULL);
- $attachment = [
- 'post_mime_type' => $wp_filetype['type'],
- 'post_parent' => $post_id,
- 'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
- 'post_content' => '',
- 'post_status' => 'inherit'
- ];
- $attachment_id = wp_insert_attachment($attachment, $fields['immagine']['raw_value'], $post_id);
- if (!is_wp_error($attachment_id)) {
- require_once(ABSPATH . "wp-admin" . '/includes/image.php');
- $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
- wp_update_attachment_metadata($attachment_id, $attachment_data);
- zbl_log($attachment_id, FALSE, "file caricato");
- update_field('servizio_immagine', $attachment_id, $post_id);
- }
- else {
- zbl_log($attachment_id->get_error_message(), FALSE, "Errore in wp_insert_attach");
- }
- }
- else {
- zbl_log($upload_file['error'], FALSE, "Errore in upload_file");
- }
- wp_update_post($post_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement