Yes it is possible to populates an acf gallery field from json data.
Lets say this is your gallery json from your app…
{
"gallery" : [
"https://i.imgur.com/VHkyr8P.jpeg",
"https://i.imgur.com/obdqDwa.jpeg",
"https://i.imgur.com/eQuSNVx.jpeg",
"https://i.imgur.com/1lVyIJt.jpeg",
"https://i.imgur.com/5uIOviX.jpeg"
]
}
Now in your cron job you could run a function as shown below to handle acf gallery field image updates.
My example function below gets any existing acf gallery images, retains them and merges new images to the gallery field…
/**
* @param $post_id int
* @param $acf_gallery_field_name string
* @param $json_url string
*/
function add_json_imgs_to_acf_gallery($post_id, $acf_gallery_field_name, $json_url) {
// get the json data via url
$json = file_get_contents($json_url);
// decode json to array
$array = json_decode($json,true);
// if decoded json is array and has array key gallery (as per above json example)
if (is_array($array) && array_key_exists('gallery')) {
// lets begin multiple attachment array
$attach_ids = [];
// foreach of your json gallery urls
foreach ($array['gallery'] as $img_url) {
// check the type of file
// we'll use this as the 'post_mime_type'
$file_type = wp_check_filetype(basename($img_url),null);
// get the path to the upload directory
$wp_upload_dir = wp_upload_dir();
// prepare an array of attachment post data
$attachment = [
'guid' => $wp_upload_dir['url'] . "https://wordpress.stackexchange.com/" . basename($img_url),
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace('/\.[^.]+$/', '',basename($img_url)),
'post_content' => '',
'post_status' => 'inherit'
];
// insert the attachment
$attach_id = wp_insert_attachment($attachment,$img_url,$post_id);
// make sure that this file is included, as wp_generate_attachment_metadata() depends on it
require_once(ABSPATH . 'wp-admin/includes/image.php');
// generate the metadata for the attachment, and update the database record
$attach_data = wp_generate_attachment_metadata($attach_id,$img_url);
wp_update_attachment_metadata($attach_id,$attach_data);
// build attachment id's as an array
$attach_ids[] = $attach_id;
}
// get our current acf gallery field images
$gallery = get_field($acf_gallery_field_name,$post_id);
// if we already have galley images
if($gallery) {
// empty array for existing gallery image id to be populated
$existing_attach_ids = [];
// for each gallery item as array key => image array
foreach ($gallery as $key => $img) {
// add image id to existing attachment ids
$existing_attach_ids[] = $img['ID'];
}
// merge existing acf gallery ids with newly json uploaded image items
$attach_ids = array_merge($existing_attach_ids,$attach_ids);
// update acf gallery field with merged attachment ids
update_field($acf_gallery_field_name,$attach_ids,$post_id);
} else {
// update acf gallery field with new attachment ids
update_field($acf_gallery_field_name,$attach_ids,$post_id);
}
}
}