升级PHP至7.4.x时WordPress Timber/Twig缓存损坏致致命错误求助
Timber/Twig + ACF Blocks 升级PHP7.4+后缓存加载致命错误解决方法
问题背景
我维护的WordPress站点基于Timber/Twig和ACF Blocks开发,本地(Windows/Laragon+PHP8)、预发布环境(共享主机A+PHP7.4.x)运行正常,但生产环境(服务器B)升级PHP到7.4.x及以上版本时,出现如下致命错误:
Fatal error: Uncaught Exception: Failed to load Twig template "templates/blocks/newsletter-form.twig", index "": cache might be corrupted in "templates/blocks/newsletter-form.twig".
in /www/[my_dir]/www/[my_domain]/public_html/wp-content/plugins/timber-library/vendor/twig/twig/src/Environment.php on line 502
调用栈详情:
Call stack: Twig\Environment::loadClass() wp-content/plugins/timber-library/vendor/twig/twig/src/Environment.php:445 Twig\Environment::loadTemplate() wp-content/plugins/timber-library/vendor/twig/twig/src/Environment.php:423 Twig\Environment::load() wp-content/plugins/timber-library/lib/Loader.php:78 Timber\Loader::render() wp-content/plugins/timber-library/lib/Timber.php:334 Timber\Timber::compile() wp-content/plugins/timber-library/lib/Timber.php:383 Timber\Timber::fetch() wp-content/plugins/timber-library/lib/Timber.php:410 Timber\Timber::render() wp-content/themes/pawilon-redux/vendor/palmiak/timber-acf-wp-blocks/timber-acf-wp-blocks.php:275 Timber_Acf_Wp_Blocks::timber_blocks_callback() wp-content/plugins/advanced-custom-fields-pro/pro/blocks.php:350 acf_render_block() wp-content/plugins/advanced-custom-fields-pro/pro/blocks.php:303 acf_rendered_block() wp-includes/class-wp-block.php:258 WP_Block::render() wp-includes/class-wp-block.php:244 WP_Block::render() wp-includes/class-wp-block.php:244 WP_Block::render() wp-includes/blocks.php:1072 render_block() wp-includes/blocks.php:1110 do_blocks() wp-content/themes/pawilon-redux/Chisel/Site.php:51 Chisel\Site::addToContext() wp-includes/class-wp-hook.php:308 WP_Hook::apply_filters() wp-includes/plugin.php:205 apply_filters() wp-content/plugins/timber-library/lib/Timber.php:269 Timber\Timber::get_context() wp-content/themes/pawilon-redux/page.php:22 include() wp-includes/template-loader.php:106 require_once() wp-blog-header.php:19 require() index.php:17
当前使用Timber版本1.22,已尝试以下方法未解决:
- 移除
templates/blocks/newsletter-form.twig模板,错误转而指向page.twig; - 调用
clear_cache_timber()和clear_cache_twig()方法清理缓存; - 彻底重装Timber插件。
解决步骤
1. 手动清理Timber缓存文件
PHP版本升级后,旧版本生成的缓存文件与新PHP环境不兼容,是最常见的原因:
- 登录服务器B的文件管理器或通过SSH,找到
wp-content/uploads/timber-cache目录; - 删除该目录下所有文件及子目录;
- 重新访问站点,Timber会自动生成适配PHP7.4+的新缓存。
2. 检查缓存目录权限
PHP版本升级可能导致文件权限变更,Timber无法写入新缓存:
- 确保
wp-content/uploads/timber-cache目录权限为755,文件权限为644; - 若使用Nginx,确认PHP运行用户(通常为
www-data)拥有该目录的读写权限。
3. 升级Timber到兼容版本
Timber 1.22发布于2021年,部分旧逻辑在PHP7.4+下存在兼容性问题:
- 升级Timber到最新稳定版(推荐2.x系列),新版本对PHP7.4+及以上版本支持更完善;
- 升级前务必备份站点及数据库,避免主题/插件兼容性冲突。
4. 临时禁用Twig缓存排查
若以上方法无效,先禁用缓存确认问题根源:
- 在主题的
functions.php中添加以下代码:
add_filter('timber/twig/environment/options', function($options) { $options['cache'] = false; return $options; });
- 访问站点,若错误消失,说明确实是缓存相关问题,再回到步骤1-3彻底解决。
5. 检查ACF Blocks插件兼容性
使用的palmiak/timber-acf-wp-blocks插件可能与PHP7.4+不兼容:
- 升级该插件至最新版本,确认是否支持PHP7.4+;
- 若插件已停止维护,可替换为官方ACF Blocks的Timber集成方案,或自行编写Block渲染逻辑。
内容的提问来源于stack exchange,提问作者ToNo
相关产品推荐
相关产品推荐

