You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP All Import自定义函数仅首次运行问题求助

WP All Import 自定义函数仅首次导入生效的问题

我写了一个自定义函数,功能是下载并解压压缩包,提取里面的所有图片上传到WordPress媒体库,最后返回URL列表,用来导入到每个待导入的产品文章里。这个函数本身功能正常,但只有首次导入(或者列表顶部的条目)时生效,其余条目的图库都是空的。想问下是不是需要添加add_filter或add_action这类钩子,让WP All Import能对所有导入的文章调用这个函数?

函数代码如下:

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);
}

问题原因与解决方法

核心问题

你的函数逻辑本身没问题,但临时文件清理步骤存在致命冲突:你用array_map('unlink', glob(get_temp_dir() . '/*'))清空了整个系统临时目录,而WP All Import批量处理时,所有条目共享这个临时目录。第一个条目处理完清空目录后,后续条目解压的图片会被直接删除,导致无法上传,最终图库为空。

解决步骤

  1. 为每个导入条目创建独立临时目录
    避免不同条目之间的文件干扰,用uniqid()生成唯一标识的子目录,替换原有的全局临时目录解压逻辑。

  2. 仅清理当前条目生成的临时文件
    不再清空整个系统临时目录,只删除当前条目专属的临时文件和目录,保证其他条目处理不受影响。

  3. WP All Import 调用方式说明
    不需要额外添加add_filter或add_action钩子,只要在WP All Import的字段映射中,用短代码形式调用函数即可:[extract_images_from_zip_and_upload({zip_file_url_field})],其中{zip_file_url_field}替换为你数据源中对应的压缩包URL字段名。确保函数定义在主题functions.php或自定义插件中,让WP All Import能加载到。

优化后的完整函数

function extract_images_from_zip_and_upload($zip_file_url) {
    // 下载压缩包,增加失败判断
    $zip_file_contents = file_get_contents($zip_file_url);
    if (!$zip_file_contents) {
        return '';
    }

    // 创建临时压缩文件
    $temp_file = tempnam(sys_get_temp_dir(), 'zip');
    file_put_contents($temp_file, $zip_file_contents);

    // 创建当前条目专属的临时解压目录
    $temp_dir = get_temp_dir() . uniqid('zip_extract_', true) . '/';
    mkdir($temp_dir, 0777, true);

    // 解压压缩包,增加失败判断
    $zip = new ZipArchive;
    if ($zip->open($temp_file) !== true) {
        unlink($temp_file);
        return '';
    }
    $zip->extractTo($temp_dir);
    $zip->close();

    // 获取当前目录下的图片文件
    $images = glob($temp_dir . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $image_urls = array();

    // 上传图片到媒体库
    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)) {
            $image_url = wp_get_attachment_url($attachment_id);
            $image_urls[] = $image_url;
        }
    }

    // 仅清理当前条目生成的临时文件
    array_map('unlink', glob($temp_dir . '/*'));
    rmdir($temp_dir);
    unlink($temp_file);

    // 返回逗号分隔的图片URL
    return implode(",", $image_urls);
}

内容的提问来源于stack exchange,提问作者RuFFCuT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 05:21:56