wordpresstwigtimber

WordPress Timber/Twig: Failed to load Twig template, cache might be corrupted fatal error when upgrading to PHP 7.4.x


I maintain a website on WordPress coded with Timber/Twig and ACF Blocks. Local installation runs on a Windows/Laragon with PHP 8 and is okay. Staging installation runs on a shared hosting A with PHP 7.4.x and is okay, too. Production however runs on a server B with PHP 7.2.x. When I try to upgrade to 7.4.x at least, I get a fatal error:

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

The site is using Timber plugin for WP, version 1.22. I've been struggling with this issue for some time and run out of ideas. To my undertanding, it's not specifically a PHP issue, since both on local and staging installations I run exactly the same code on newer PHPs with no problems. (And yes, I know, that staging should theoretically be runned on the same hosting as production).

I tried to:

  1. Remove templates/blocks/newsletter-form.twig template, but then the problem jumps further to page.twig
  2. Clear caches with clear_cache_timber() and clear_cache_twig() methods
  3. Reinstalling Timber plugin from scratch

None of the above worked. Obviously, keeping the website on PHP 7.2.x is not an option in the long run, so I need to solve this problem somehow.

Have any on you encountered a similar issue and found a working solution?


Solution

  • I figured out, what was the problem. The issue was the use of eval() function. The server I have website on has this function disabled due to security reasons. That was the first thing. The second one was that I had Timber caching disabled. Those two combined caused the problem. Twig renderer, while creating the classes responsible for rendering the templates, tried to access cached files (which did not exist) and in the last resort tried to create those using eval(). None of those was successfull, which caused the renderer to try to use non-existent class, thus resulting in fatal error and website crash.

    Solution: I enabled Timber caching.