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

WordPress/WooCommerce商品图片重命名:以商品标题设置图片信息

问题:WooCommerce商品图片自动命名及元信息设置

我正在为WooCommerce店铺重命名图片,当前媒体库的图片都是123456.png、main.jpg这类无意义命名。想要通过PHP代码实现:上传图片时,自动将图片的文件名、描述、Alt文本设置为该图片“上传至”对应的商品标题。试过网上的代码和插件都没解决,自己写的代码也无效,求帮助。


原代码存在的问题

  1. file_renamer函数获取到商品标题后,最后仍返回原文件名,导致命名逻辑完全不生效
  2. $my_image_title = $post;这行错误地将post对象直接赋值,后续拼接文件名会引发错误
  3. 未对商品标题做文件名合规处理,特殊字符可能导致上传失败
  4. 仅依赖$_POST['post_id']获取关联商品ID,兼容场景有限

修正后的完整代码

// 重命名上传的图片文件名为关联商品标题
add_filter('sanitize_file_name', 'wc_rename_product_image_file', 10, 2);
function wc_rename_product_image_file($filename, $filename_raw) {
    // 仅处理图片格式文件
    $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif', 'webp');
    $info = pathinfo($filename);
    $ext = strtolower($info['extension'] ?? '');
    
    if (!in_array($ext, $allowed_extensions)) {
        return $filename;
    }

    // 获取关联的商品ID(兼容两种上传场景)
    $post_id = null;
    // 场景1:商品编辑页直接上传图片
    if (isset($_REQUEST['post_id'])) {
        $post_id = absint($_REQUEST['post_id']);
    }
    // 场景2:媒体库上传时选择“上传至”商品
    elseif (isset($_POST['post_id'])) {
        $post_id = absint($_POST['post_id']);
    }

    // 验证是否为WooCommerce商品
    if ($post_id && get_post_type($post_id) === 'product') {
        $product_title = get_the_title($post_id);
        // 格式化标题为合规文件名
        $sanitized_title = sanitize_title_with_dashes($product_title);
        // 添加唯一ID避免同名文件覆盖
        $new_filename = $sanitized_title . '-' . uniqid() . '.' . $ext;
        return $new_filename;
    }

    // 无关联商品时保留原文件名
    return $filename;
}

// 自动设置图片的标题、Alt文本、说明、描述为商品标题
add_action('add_attachment', 'wc_set_product_image_meta');
function wc_set_product_image_meta($attachment_id) {
    if (!wp_attachment_is_image($attachment_id)) {
        return;
    }

    // 获取图片关联的父商品ID(WordPress自动记录的关联关系)
    $parent_post_id = wp_get_post_parent_id($attachment_id);
    if (!$parent_post_id || get_post_type($parent_post_id) !== 'product') {
        return;
    }

    $product_title = get_the_title($parent_post_id);
    // 格式化标题(可选,可根据需求调整规则)
    $formatted_title = ucwords(strtolower(preg_replace('%\s*[-_\s]+\s*%', ' ', $product_title)));

    // 更新图片基础元信息
    wp_update_post(array(
        'ID' => $attachment_id,
        'post_title' => $formatted_title,      // 图片标题
        'post_excerpt' => $formatted_title,    // 图片说明(媒体库的“说明”字段)
        'post_content' => $formatted_title,    // 图片描述(媒体库的“描述”字段)
    ));

    // 更新图片Alt文本
    update_post_meta($attachment_id, '_wp_attachment_image_alt', $formatted_title);
}

代码关键说明

  • 文件名处理:
    • 仅针对图片格式生效,不干扰其他文件上传
    • 兼容两种常见上传场景:商品编辑页直接上传、媒体库上传时指定“上传至”商品
    • 用sanitize_title_with_dashes处理标题,确保文件名符合服务器规则,避免特殊字符报错
    • 添加uniqid()避免同一商品的多张图片上传时互相覆盖
  • 元信息设置:
    • 通过wp_get_post_parent_id获取关联商品ID,比直接读取请求参数更可靠(WordPress会自动记录图片与父文章的关联)
    • 仅处理关联WooCommerce商品的图片,不影响其他文章的图片
    • 统一设置所有需要的图片元信息,也可根据需求单独注释掉某一项(比如不需要修改描述就删掉post_content行)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 09:43:16