【问题标题】:How do I unhook WordPress action hook in plugin file?如何解开插件文件中的 WordPress 动作挂钩?
【发布时间】:2016-08-02 02:54:57
【问题描述】:

我正在尝试从我的子主题 functions.php 文件中取消挂钩并修改操作。

WordPress 插件 Sensei 在本文档的第 86 行添加了此操作。

https://github.com/Automattic/sensei/blob/master/includes/class-sensei-modules.php#L86

该操作引用了页面下方负责输出动态标题元素的函数。

/**
 * Show the title modules on the single course template.
 *
 * Function is hooked into sensei_single_course_modules_before.
 *
 * @since 1.8.0
 * @return void
 */

public function course_modules_title( ) {
   if( sensei_module_has_lessons() ){
        echo '<header><h2>' . __('Modules', 'woothemes-sensei') . '</h2></header>';
    }
}

我的目标是将当前输出为“模块”的 html 更改为其他内容。

我在我的子主题 functions.php 文件中尝试了以下操作,但似乎都没有工作。

remove_action( 'sensei_single_course_modules_before', array( 'Sensei_Core_Modules', 'course_modules_title' ), 20);

remove_action( 'sensei_single_course_modules_before', array( 'Sensei()->Sensei_Core_Modules', 'course_modules_title' ), 20);

问题是,我不知道如何确定将哪个初始参数添加到数组中以调用正确的类。因为我是在外部访问它,所以我不能像在核心文件中那样使用$this

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    为了移除动作,你必须找到类的实例。这是一个假设,因为我无法访问 Sensei 的源代码,但很有可能有一个,因为大多数 WordPress 插件都使用这种方法。

    找到实例名称后,您可以使用 global $senseiInstance 加载它 - 将其替换为变量的实际名称。

    然后您可以使用以下代码删除该操作:

    remove_action( 'sensei_single_course_modules_before', array( $senseiInstance, 'course_modules_title' ), 20);

    更多信息可以在例如这篇文章中找到:https://www.sitepoint.com/digging-deeper-wordpress-hooks-filters

    希望对你有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多