如何修复Elementor升级至3.10后出现的Undefined array key "file"警告
Elementor升级3.10后出现Undefined array key "file"错误的解决办法
问题情况
- 将Elementor从3.9.2升级到3.10后,首页底部持续弹出PHP警告:
Warning: Undefined array key "file" in (...)/wp-includes/media.php on line 1680,回滚至3.9.2版本后警告消失。 - 首页首节设置了短视频作为背景。
- 报错对应的原代码(位于
wp-includes/media.php第1680行附近):
// Bail early if an image has been inserted and later edited. if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { return $image; }
原因分析
错误核心是代码直接访问了$image_meta数组中不存在的"file"键。由于首页用短视频作为背景,Elementor 3.10的媒体处理逻辑可能误将短视频资源当作图片处理,导致获取到的元数据里没有图片特有的"file"字段,进而触发警告。
正确解决方案
不要用@符号屏蔽错误(这是不推荐的临时方案,会掩盖其他潜在问题),应该先检查数组键是否存在,再执行后续逻辑。修改代码如下:
// Bail early if an image has been inserted and later edited. if ( isset( $image_meta['file'] ) && preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { return $image; }
修改说明:在正则匹配前添加isset( $image_meta['file'] )判断,确保"file"键存在后再执行后续操作,彻底避免Undefined array key警告。
内容的提问来源于stack exchange,提问作者Weronika Pawlik
相关产品推荐
相关产品推荐

