Function only running once per import
-
I’ve created a function that long story short extracts a zip file, adds the images to the media library and returns a list of the image URLs to a WP all import ACF gallery field, and it works perfectly, the problem is it only works perfectly for one of the imported items – it seems to run once and not again. Am I missing something? I can’t see anything in the documentation about it.
Function code is:function extract_images_from_zip_and_upload($zip_file_url) { // Download the zip file $zip_file_contents = file_get_contents($zip_file_url); // Create a temporary file to store the contents $temp_file = tempnam(sys_get_temp_dir(), 'zip'); // Write the contents to the temporary file file_put_contents($temp_file, $zip_file_contents); // Extract the zip file $zip = new ZipArchive; $zip->open($temp_file); $zip->extractTo(get_temp_dir()); $zip->close(); // Get all the image files from the extracted folder $images = glob(get_temp_dir() . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE); // Array to store URLs of the uploaded images $image_urls = array(); // Loop through the image files and add them to the media library foreach ($images as $image) { $file_array = array( 'name' => basename($image), 'tmp_name' => $image ); $attachment_id = media_handle_sideload($file_array, 0); if (is_wp_error($attachment_id)) { continue; } $image_url = wp_get_attachment_url($attachment_id); $image_urls[] = $image_url; } // Clean up array_map('unlink', glob(get_temp_dir() . '/*')); unlink($temp_file); //Return the URLs of the uploaded images separated by commas return implode(",", $image_urls); }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Function only running once per import’ is closed to new replies.