如何在插件文件中解除WordPress操作挂钩


How do I unhook WordPress action hook in plugin file?

我正试图从我的子主题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更改为其他内容。

我在我的child主题函数.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那样使用它。

为了删除操作,您必须找到该类的实例。这是一个假设,因为我无法访问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.

希望这能帮到你!