Wordpress - 从 Timber Plugin 1.x 切换到基于 Composer 的 Timber 1.x 错误致命错误:未捕获的 LogicException:无法注册扩展

问题描述 投票:0回答:1

我目前正在尝试将 Timber 的 ouf 网站版本切换为其 Composer 版本。通过在 function.php 文件中初始化 Timber,我设法加载供应商并使管理员毫无问题地返回。但我们的起始站点类似乎存在问题,更准确地说是在“add_to_twig”函数中,并且抛出的错误对我没有多大帮助...... 为了向此请求添加更多上下文,该网站是由其他人使用 Timber 的插件版本开发的,因此很难真正指出问题出在哪里......

我的所有自定义帖子类型和所有内容都在管理端正确显示,但是当尝试渲染页面时,它会抛出此错误:“致命错误:未捕获的逻辑异常:无法注册扩展“Twig\Extension\StringLoaderExtension”已经注册。在 /var/www/html/wp-content/themes/dld/vendor/twig/twig/src/ExtensionSet.php 第 149 行”

我在“add_to_twig”函数中加载此扩展,并且我尝试以这种方式添加的每个过滤器都会引发此错误。我搜索是否有其他地方可以加载此内容,但没有找到。 (并且它在插件版本中工作得很好......) 这让我觉得这可能与我在主题中加载 Timber 的方式有关,但通过与 Timber 1.x 的起始主题进行比较,我看不出问题。

我是瞎子吗?

我发布了函数 php 代码以及 StarterSite 文件如何集成的部分:

<?php

/**
 * Timber DLD Theme
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */

 $composer_autoload = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $composer_autoload ) ) {
    require_once $composer_autoload;
    $timber = new Timber\Timber();
}


if ( ! class_exists( 'Timber' ) ) {
    add_action( 'admin_notices', function() {
        echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
    });

    add_filter('template_include', function( $template ) {
        return get_stylesheet_directory() . '/static/no-timber.html';
    });

    return;
}

/**
 * Sets the directories (inside your theme) to find .twig files
 */
Timber::$dirname = array( 'templates', 'views' );

/**
 * By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
 * No prob! Just set this value to true
 */
Timber::$autoescape = false;


include( "lib/DLDSite.php" );
new DLDSite();

以及我的 StarterSite 类的摘录:

<?php

/**
 * We're going to configure our theme inside of a subclass of Timber\Site
 * You can move this to its own file and include here via php's
 * include("MySite.php")
 */

class DLDSite extends Timber\Site {

  const VERSION = '1.2.1';
  const IS_PROD = 'production';

  /** Add timber support. */
  public function __construct() {
    add_action('after_setup_theme', [$this, 'theme_supports']);
    add_filter('timber/context', [$this, 'add_to_context']);
    add_filter('timber/twig', [$this, 'add_to_twig']);
    add_action('init', [$this, 'hide_editor']);
    add_action('init', [$this, 'register_taxonomies']);
    add_action('init', [$this, 'register_post_types']);
    add_action( 'init', [$this, 'disable_emojis'] );
    add_action('acf/init', [$this, 'register_acf_options_pages']);

    add_action('parse_query', [$this, 'no_nopaging_functionality_index']);

    add_filter('post_type_archive_title', [$this, 'functionality_archive_title_filter'], 11, 3);
    add_filter('bcn_breadcrumb_title', [$this, 'homepage_title_filter'], 10, 3);
    
    add_filter( 'gform_pre_validation', 'gf_check_uppercase_email' );
    add_filter('http_request_timeout', function ($timeout) {
      $newtimeout = 10;
      return $newtimeout;
    });

    foreach(get_field_object('field_5d7a40723c983')['choices'] as $choices){
      do_action( 'wpml_register_single_string', 'acfcustom', 'manquement choices - '.$choices, $choices );
    }
    add_action( 'save_post_sanction', array($this, 'invalidate_sanction_transient') );
    add_action( 'admin_menu', 'addSanctionUpdateMenu' );
    
    if (self::IS_PROD) {
      // Patch Gravity Forms for jquery async/defer issue
      
      add_action('wp_enqueue_scripts', [$this, 'add_theme_sources'], 20);
    } else {
      add_action('wp_enqueue_scripts', [$this, 'add_theme_sources'], 20);
    }

    parent::__construct();
  }
//...

/** This is where you can add your own functions to twig.
   *
   * @param string $twig get extension.
   */
  public function add_to_twig($twig) {
    $twig->addExtension( new Twig\Extension\StringLoaderExtension() );
    $twig->addFilter(new Twig_SimpleFilter('remove_suggestion_and_loop', array( $this, 'remove_suggestion_and_loop' )));
    $twig->addFilter(new Twig_SimpleFilter('remove_suggestion_and_loop_autoplay', array( $this, 'remove_suggestion_and_loop_autoplay' )));
    $twig->addFilter(new Twig_SimpleFilter('remove_suggestion_and_loop_autoplay_popup', array( $this, 'remove_suggestion_and_loop_autoplay_popup' )));
    $twig->addFilter(new Twig_SimpleFilter('embed_video_all_params', array( $this, 'embed_video_all_params' )));
    $twig->addFilter(new Twig_SimpleFilter('shuffle', array( $this, 'shuffle' )));
    return $twig;
  }

该文件很长,所以我只在这里摘录一段。我这里有更多自定义功能,但主要是向管理中的菜单或选项添加新项目的功能,因此它不应该干扰 Timber(并且它正在与插件版本一起使用,所以......)

如果错误很明显,我真的很抱歉,但在尝试了几个小时来完成这项工作后,我没有任何想法。

完整的错误消息是:“致命错误:未捕获的LogicException:无法注册扩展“Twig\Extension\StringLoaderExtension”,因为它已经注册。在 /var/www/html/wp-content/themes/dld/vendor/twig /twig/src/ExtensionSet.php 第 149 行 LogicException:无法注册扩展“Twig\Extension\StringLoaderExtension”,因为它已注册。在 /var/www/html/wp-content/themes/dld/vendor/twig/ 中twig/src/ExtensionSet.php 第 149 行”。当我从“add_to_timber”中删除 StringLoaderExtension 时,我尝试加载的下一个过滤器会触发此错误等。

有关该网站的更多信息: 我正在运行 Wordpress V.6.4.1 PhP 8.0 在尝试激活 Composer 版本之前,我已停用 Timber 的插件版本。 我有一些插件,例如 Gravity Forms、WPML 或 ACF,它们应该是最新的(或最多 2-3 周)

谢谢你!

wordpress twig timber twig-filter
1个回答
0
投票

感谢 Darkbee,我想我找到了罪魁祸首。在我从开发人员那里获得网站时收到的代码中,有几个实例定义了新的StartingClass,以便可以快速请求使用其中的模板。 像这样的东西: $startingClass = 新的StartingClass(); $myVar = $startingClass->startingClassFunction();

当删除对此的所有引用时,模板加载正常(除了上面代码查询的部分)。我的猜测是,每次使用它时它都会调用我的起始类构造函数,所以是的,扩展(以及其他所有内容)确实被加载了不止一次。

很奇怪,它适用于 Timber 的插件版本,而不是作曲家版本,但至少我现在可以进步了。

再次感谢Darkbee的提示!

祝你有美好的一天

© www.soinside.com 2019 - 2024. All rights reserved.