如何从基于类的WordPress插件中删除一个钩子
我将向您展示如何从另一个第三方WordPress插件中删除操作或过滤器挂钩,即使该插件是基于类和面向对象的。
在这篇文章中,我将向您展示如何从另一个插件中删除动作或过滤器钩子,即使该插件是基于类的。
基本删除操作和过滤器
你可能已经知道,你可以删除某人的ADD_ACTION () 使用remove_action () ,你可以删除某人的add_filter () 使用remove_filter () 。
所以,在正常情况下,它非常简单。在某人的插件中,他们有类似......
1
2
|
add_action('save_post', 'my_action_function');
add_filter('the_content', 'my_filter_function');
|
...而你只是用...删除它们
1
2
|
remove_action('save_post', 'my_action_function');
remove_filter('the_content', 'my_filter_function');
|
基于类的插件有点复杂
使用类的插件看起来像这样......
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class My_Plugin_Class {
function __construct() {
add_action( 'save_post', array( $this, 'my_action_function' ), 11);
}
function my_action_function(){
//do something
}
}
$my_plugin_class = new My_Plugin_Class();
|
请注意,add_action不仅具有函数回调的简单字符串,而是具有带有$ this的数组,然后是该函数回调字符串。这允许插件开发人员使用简单的函数名称而不必担心与其他插件冲突,因为它们仅限于该类的范围。
那么,你如何删除它?
如何从基于类的插件中删除挂钩
如果您可以挂钩到该插件,那么您应该能够使用此方法来访问类对象实例以删除挂钩。
1
2
|
global $my_plugin_class; // Get access to the class object instance
remove_action('save_post', array($my_plugin_class, 'my_action_function'), 11); // Remove the class hook using the same priority
|
如何从基于类的插件外部删除钩子
如果你无法挂钩基于类的插件,那么尝试在回调数组的第一部分使用类名作为字符串,就像这样...
1
|
remove_action('save_post', array('My_Plugin_Class', 'my_action_function'), 11); // Remove the class hook using the same priority
|
如果这些方法都不起作用,请使用这些函数
有时,它不会起作用。但是,这里有一些很好的辅助功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php
/**
* Make sure the function does not exist before defining it
*/
if( ! function_exists( 'remove_class_filter' ) ){
/**
* Remove Class Filter Without Access to Class Object
*
* In order to use the core WordPress remove_filter() on a filter added with the callback
* to a class, you either have to have access to that class object, or it has to be a call
* to a static method. This method allows you to remove filters with a callback to a class
* you don't have access to.
*
* Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
* Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
*
* @param string $tag Filter to remove
* @param string $class_name Class name for the filter's callback
* @param string $method_name Method name for the filter's callback
* @param int $priority Priority of the filter (default 10)
*
* @return bool Whether the function is removed.
*/
function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
global $wp_filter;
// Check that filter actually exists first
if ( ! isset( $wp_filter[ $tag ] ) ) {
return FALSE;
}
/**
* If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
* a simple array, rather it is an object that implements the ArrayAccess interface.
*
* To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
*
* @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
*/
if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
// Create $fob object from filter tag, to use below
$fob = $wp_filter[ $tag ];
$callbacks = &$wp_filter[ $tag ]->callbacks;
} else {
$callbacks = &$wp_filter[ $tag ];
}
// Exit if there aren't any callbacks for specified priority
if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) {
return FALSE;
}
// Loop through each filter for the specified priority, looking for our class & method
foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) {
// Filter should always be an array - array( $this, 'method' ), if not goto next
if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) {
continue;
}
// If first value in array is not an object, it can't be a class
if ( ! is_object( $filter['function'][0] ) ) {
continue;
}
// Method doesn't match the one we're looking for, goto next
if ( $filter['function'][1] !== $method_name ) {
continue;
}
// Method matched, now let's check the Class
if ( get_class( $filter['function'][0] ) === $class_name ) {
// WordPress 4.7+ use core remove_filter() since we found the class object
if ( isset( $fob ) ) {
// Handles removing filter, reseting callback priority keys mid-iteration, etc.
$fob->remove_filter( $tag, $filter['function'], $priority );
} else {
// Use legacy removal process (pre 4.7)
unset( $callbacks[ $priority ][ $filter_id ] );
// and if it was the only filter in that priority, unset that priority
if ( empty( $callbacks[ $priority ] ) ) {
unset( $callbacks[ $priority ] );
}
// and if the only filter for that tag, set the tag to an empty array
if ( empty( $callbacks ) ) {
$callbacks = array();
}
// Remove this filter from merged_filters, which specifies if filters have been sorted
unset( $GLOBALS['merged_filters'][ $tag ] );
}
return TRUE;
}
}
return FALSE;
}
}
/**
* Make sure the function does not exist before defining it
*/
if( ! function_exists( 'remove_class_action') ){
/**
* Remove Class Action Without Access to Class Object
*
* In order to use the core WordPress remove_action() on an action added with the callback
* to a class, you either have to have access to that class object, or it has to be a call
* to a static method. This method allows you to remove actions with a callback to a class
* you don't have access to.
*
* Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
*
* @param string $tag Action to remove
* @param string $class_name Class name for the action's callback
* @param string $method_name Method name for the action's callback
* @param int $priority Priority of the action (default 10)
*
* @return bool Whether the function is removed.
*/
function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
remove_class_filter( $tag, $class_name, $method_name, $priority );
}
}
|
在代码中提供这些功能,您可以像这样使用它们......
1
|
remove_class_action('My_Plugin_Class', 'my_action_function', 11); // Remove the class hook using the same priority
|
看了上面的内容您还有疑问吗?
希望这篇文章解决了你的问题。如果您有任何疑问,请在下面的评论中@我。
这个文章解决了我的问题,谢谢
来看的