• I’m aiming to grab all the media files (images mostly) used in a WordPress blog and upload them to a site being set up on Shopify. I managed to extract all the image IDs but not URLs. But not sure how to proceed from here. I don’t want the entire media library, just the images used in published posts. It looks like about 300 images in all.

    Any tips for accomplishing this efficiently?

    Thank you.

Viewing 1 replies (of 1 total)
  • Since you already have the image IDs, this makes it easy. Just copy the code snippet below into your theme’s functions.php file, then refresh your homepage once. It will immediately deliver a ZIP file containing all your images.

    // === Function to convert array of IDs to a ZIP file ===
    function ids_to_zip($ids, $name = 'images') {
    if (!class_exists('ZipArchive')) wp_die('ZipArchive missing');
    $zip = new ZipArchive;
    $tmp = sys_get_temp_dir() . '/' . $name . '.zip';
    if ($zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) wp_die('Cannot create zip');
    foreach ($ids as $id) {
    $file = get_attached_file($id);
    if ($file) $zip->addFile($file, basename($file));
    }
    $zip->close();
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="' . $name . '.zip"');
    readfile($tmp);
    unlink($tmp);
    exit;
    }

    // === How to use it – replace the IDs with your own ===
    $my_ids = array(101, 102, 103, 104); // ← Put your actual image IDs here
    ids_to_zip($my_ids, 'my-300-images');

    Note: After refreshing the homepage once, the ZIP file will automatically download. To avoid re-running the code on every page load, remove or comment out the usage lines after your download is complete.

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.