【问题标题】:Wordpress - plug-in functions is available in templates but not available to other plug-insWordpress - 插件功能在模板中可用,但对其他插件不可用
【发布时间】:2013-05-16 22:47:49
【问题描述】:

案例:

插件 A 指定了一个可以在 WP 模板中调用的函数。

但是,从插件 B 的代码中调用相同的函数会产生错误:

致命错误:调用未定义函数...

我假设问题与包含和/或范围有关,但到目前为止还没有找到答案。简单地要求或包含包含所需功能的插件 A php 文件会在其中产生“重新声明”错误(插件 A php 文件)。

【问题讨论】:

    标签: php scope wordpress


    【解决方案1】:

    如果插件 B 依赖于插件 A 的函数,您可以在所有活动插件时调用的 init 函数中启动插件 B 代码已使用 WordPress 中的 plugins_loaded 操作加载。

    您可以找到here 的操作列表。

    此示例假设您正在使用类编写插件:

    在插件 B 中:

    function __construct() {
        add_action( 'plugins_loaded', array( $this, 'init' ) );
    }
    
    function init() {
        // functions from plugin A will be available from this point on
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      相关资源
      最近更新 更多