<?php
// post ID
$post_id = 123;
// reference of the post, the images will be uploaded in a folder named after it, could be replaced by the post ID if you don't have a reference
$reference = 'TEST-REF';
// array of images URLs to attach
$images = array(
'https://static5.immomigsa.ch/s/710e6/1/pictures/objects/1_5e6895ada6a599.35609643.jpg',
'https://static3.immomigsa.ch/s/c9e4c/1/pictures/objects/1_5e6895ade6a061.09425577.jpg',
'https://static3.immomigsa.ch/s/11761/1/pictures/objects/1_5e6895ae1bba80.89092542.jpg',
);
// key of the ACF to update
$acf_key = 'images';
// declaring upload base directory, images will be uploaded within subfolder of this folder, created un the "uploads" folder
$base_upload_folder = 'immomig';
// getting WP uploads path
$wp_upload_path = wp_upload_dir()['basedir'];
// if base folder doesn't exist, it is created
if (!is_dir($wp_upload_path.'/'.$base_upload_folder.'/'))
mkdir($wp_upload_path.'/'.$base_upload_folder.'/');
// removing the previously attached images
$attachedz = get_field_object($acf_key, $post_id, false, true); // getting the list of images linked to this post through this ACF
if ($attachedz[0]) { // if there are several in the main array...
foreach($attachedz as $attached) // ...then for each...
wp_delete_attachment(is_numeric($attached['value'])?$attached['value']:$attached['ID'], true); // ...we delete it
}
elseif (is_array($attachedz['value'])) { // or if there are several in a sub array...
foreach($attachedz['value'] as $attached_id) // ...then for each...
wp_delete_attachment($attached_id, true); // ...we delete it
}
else { // or if there is a single one...
wp_delete_attachment(is_numeric($attachedz['value'])?$attachedz['value']:$attachedz['ID'], true); // ...we delete it
}
// initiating array of attached images IDs
$attach_idz = array();
// looping on each image URL
foreach($images as $url) {
// getting filename
$filename = basename($url);
// getting upload path directory
$upload_path_dir = $wp_upload_path.'/'.$base_upload_folder.'/'.$reference.'/';
// if directory doesn't existe, it is created
if (!is_dir($upload_path_dir))
mkdir($upload_path_dir);
// getting complete upload path for the file
$upload_path = $upload_path_dir.$filename;
// if a file with the same name already exists, it is renamed with a trailing number
$n = 2;
while(is_file($upload_path))
$upload_path = $upload_path_dir.pathinfo($filename,PATHINFO_FILENAME).'-'.($n++).'.'.pathinfo($filename,PATHINFO_EXTENSION);
// uploading file to folder
file_put_contents($upload_path, file_get_contents($url));
// getting file type
$filetype = wp_check_filetype(basename($upload_path), null);
// iniatiating attachment datas
$attachment = array(
'guid' => $upload_path,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
// attaching the file to the post
$attach_id = wp_insert_attachment($attachment, $upload_path, $post_id);
// loading admin image library
require_once(ABSPATH.'wp-admin/includes/image.php');
// genertating attachment metadata
$attach_data = wp_generate_attachment_metadata($attach_id, $upload_path);
// updating metadata on the attached image
wp_update_attachment_metadata($attach_id, $attach_data);
// adding attachment ID to the array of attached images IDs
$attach_idz[] = $attach_id;
}
// updating ACF gallery with images IDs
update_field($acf_key, $attach_idz, $post_id);
Comments