Advertisement
rAthus

[WordPress] programmatically create ACF images gallery with PHP

Mar 16th, 2021 (edited)
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2.    
  3.     // post ID
  4.     $post_id = 123;
  5.     // 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
  6.     $reference = 'TEST-REF';
  7.     // array of images URLs to attach
  8.     $images = array(
  9.         'https://static5.immomigsa.ch/s/710e6/1/pictures/objects/1_5e6895ada6a599.35609643.jpg',
  10.         'https://static3.immomigsa.ch/s/c9e4c/1/pictures/objects/1_5e6895ade6a061.09425577.jpg',
  11.         'https://static3.immomigsa.ch/s/11761/1/pictures/objects/1_5e6895ae1bba80.89092542.jpg',
  12.     );
  13.    
  14.     // key of the ACF to update
  15.     $acf_key = 'images';
  16.    
  17.     // declaring upload base directory, images will be uploaded within subfolder of this folder, created un the "uploads" folder
  18.     $base_upload_folder = 'immomig';
  19.    
  20.     // getting WP uploads path
  21.     $wp_upload_path = wp_upload_dir()['basedir'];
  22.     // if base folder doesn't exist, it is created
  23.     if (!is_dir($wp_upload_path.'/'.$base_upload_folder.'/'))
  24.         mkdir($wp_upload_path.'/'.$base_upload_folder.'/');
  25.     // removing the previously attached images
  26.     $attachedz = get_field_object($acf_key, $post_id, false, true); // getting the list of images linked to this post through this ACF
  27.     if ($attachedz[0]) { // if there are several in the main array...
  28.         foreach($attachedz as $attached) // ...then for each...
  29.             wp_delete_attachment(is_numeric($attached['value'])?$attached['value']:$attached['ID'], true); // ...we delete it
  30.     }
  31.     elseif (is_array($attachedz['value'])) { // or if there are several in a sub array...
  32.         foreach($attachedz['value'] as $attached_id) // ...then for each...
  33.             wp_delete_attachment($attached_id, true); // ...we delete it
  34.     }
  35.     else { // or if there is a single one...
  36.         wp_delete_attachment(is_numeric($attachedz['value'])?$attachedz['value']:$attachedz['ID'], true); // ...we delete it
  37.     }
  38.     // initiating array of attached images IDs
  39.     $attach_idz = array();
  40.     // looping on each image URL
  41.     foreach($images as $url) {
  42.         // getting filename
  43.         $filename = basename($url);
  44.         // getting upload path directory
  45.         $upload_path_dir = $wp_upload_path.'/'.$base_upload_folder.'/'.$reference.'/';
  46.         // if directory doesn't existe, it is created
  47.         if (!is_dir($upload_path_dir))
  48.             mkdir($upload_path_dir);
  49.         // getting complete upload path for the file
  50.         $upload_path = $upload_path_dir.$filename;
  51.         // if a file with the same name already exists, it is renamed with a trailing number
  52.         $n = 2;
  53.         while(is_file($upload_path))
  54.             $upload_path = $upload_path_dir.pathinfo($filename,PATHINFO_FILENAME).'-'.($n++).'.'.pathinfo($filename,PATHINFO_EXTENSION);
  55.         // uploading file to folder
  56.         file_put_contents($upload_path, file_get_contents($url));
  57.         // getting file type
  58.         $filetype = wp_check_filetype(basename($upload_path), null);
  59.         // iniatiating attachment datas
  60.         $attachment = array(
  61.             'guid'           => $upload_path,
  62.             'post_mime_type' => $filetype['type'],
  63.             'post_title'     => preg_replace('/\.[^.]+$/', '', $filename),
  64.             'post_content'   => '',
  65.             'post_status'    => 'inherit'
  66.         );
  67.         // attaching the file to the post
  68.         $attach_id = wp_insert_attachment($attachment, $upload_path, $post_id);
  69.         // loading admin image library
  70.         require_once(ABSPATH.'wp-admin/includes/image.php');
  71.         // genertating attachment metadata
  72.         $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path);
  73.         // updating metadata on the attached image
  74.         wp_update_attachment_metadata($attach_id, $attach_data);
  75.         // adding attachment ID to the array of attached images IDs
  76.         $attach_idz[] = $attach_id;
  77.     }
  78.    
  79.     // updating ACF gallery with images IDs
  80.     update_field($acf_key, $attach_idz, $post_id);
  81.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement