WooCommerce REST API创建商品时WebP图片上传失败,如何解决?
解决WooCommerce API上传WebP图片报错的方法
1. 开启WordPress对WebP文件的上传支持
WordPress默认限制WebP格式上传,需添加代码到主题的functions.php文件或自定义功能插件:
// 允许WebP文件上传 function allow_webp_uploads( $mime_types ) { $mime_types['webp'] = 'image/webp'; return $mime_types; } add_filter( 'upload_mimes', 'allow_webp_uploads' ); // 支持WebP生成缩略图 function enable_webp_thumbnail_support() { add_filter( 'wp_image_editors', function( $editors ) { array_unshift( $editors, 'WP_Image_Editor_WebP' ); return $editors; } ); } add_action( 'init', 'enable_webp_thumbnail_support' );
2. 确认服务器环境支持WebP
检查服务器是否安装了支持WebP的图片处理扩展:
- GD库需版本2.0.34及以上
- Imagick需安装WebP插件
可通过WordPress后台工具 > 站点健康查看服务器环境详情。
3. 规范API请求中的WebP图片参数
调用WooCommerce API时,需明确指定图片的MIME类型为image/webp,示例REST API请求:
{ "name": "测试商品", "type": "simple", "regular_price": "29.99", "images": [ { "src": "https://your-domain.com/path/to/your-image.webp", "mime_type": "image/webp" } ] }
若使用本地文件上传(multipart/form-data格式),需在表单字段中指定正确的MIME类型。
4. 升级WooCommerce和WordPress版本
旧版本可能存在WebP兼容性问题,确保使用最新版的WooCommerce和WordPress。
内容的提问来源于stack exchange,提问作者Reza Zolfaghari
相关产品推荐
相关产品推荐

