-
Notifications
You must be signed in to change notification settings - Fork 3
/
remove-hooks.php
92 lines (79 loc) · 2.27 KB
/
remove-hooks.php
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
<?php
/**
* Demonstrate how the actions can be removed for each example.
*/
/*
* Procedural
* removes examples/procedural.php
*/
remove_action( 'init', 'hookex_init' );
/*
* Instantiated object
* removes examples/object.php
*/
global $hookex_object; // Declare global just to be safe or use in a different scope.
remove_action( 'init', array( $hookex_object, 'init' ) );
/*
* Singleton
* removes examples/singleton.php
*/
$singleton = Hookex_Singleton::get_instance();
remove_action( 'init', array( $singleton, 'init' ) );
/*
* Static class
* removes examples/static-class.php
*/
remove_action( 'init', array( 'Hookex_Static_Class', 'init1' ) );
remove_action( 'init', array( 'Hookex_Static_Class', 'init2' ) );
/*
* Mixed class
* removes examples/mixed-class.php
*/
global $hookex_mixed;
remove_action( 'init', array( $hookex_mixed, 'init1' ) );
remove_action( 'init', array( 'Hookex_Mixed_Class', 'init2' ) );
/*
* The example in examples/inaccessible-object.php takes a bit more effort...
* To accomplish it, we'll create a helper function:
*/
/**
* Remove an anonymous object filter.
*
* @link http://wordpress.stackexchange.com/a/57088 WordPress Stackexchange answer
*
* @param string $tag Hook name.
* @param string $class Class name
* @param string $method Method name
* @param int $priority Hook priority
*/
function hookex_remove_anonymous_object_filter( $tag, $class, $method, $priority = 10 ) {
global $wp_filter;
if ( empty( $wp_filter[ $tag ] ) ) {
return;
}
// Clone array so original array pointer is preserved
$filters = $wp_filter[ $tag ];
foreach ( $filters as $_priority => $filter ) {
if ( $_priority !== $priority ) {
continue;
}
foreach ( $filter as $identifier => $function ) {
if (
is_array( $function )
&& is_array( $function['function'] )
&& is_a( $function['function'][0], $class )
&& $method === $function['function'][1]
) {
remove_filter( $tag, array( $function['function'][0], $method ), $_priority );
}
}
}
}
/*
* Now we'll remove the filter added by the inaccessible object
* removes examples/inaccessible-object.php
*/
hookex_remove_anonymous_object_filter( 'init', 'Hookex_Inaccessible_Object', 'init' );
/*
* The example in examples/closure.php cannot be __reliably__ unhooked. :(
*/