-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoving-Actions-and-Filters.php
More file actions
65 lines (45 loc) · 2.37 KB
/
Removing-Actions-and-Filters.php
File metadata and controls
65 lines (45 loc) · 2.37 KB
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
To remove a callback function from a hook, you need to call remove_action() or remove_filter(), depending whether the callback function was added as an Action or a Filter.
The parameters passed to remove_action() / remove_filter() must be identical to the parameters passed to add_action() / add_filter() that registered it, or the removal won’t work.
#Example
Lets say we want to improve the performance of a large theme by removing unnecessary functionality.
Let’s analyze the theme’s code by looking into functions.php
function wporg_setup_slider() {
// ...
}
add_action( 'template_redirect', 'wporg_setup_slider', 9 );
The wporg_setup_slider function is adding a slider that we don’t need, which probably loads a huge CSS file followed by a JavaScript initialization file which uses a custom written library the size of 1MB. We can can get rid of that.
Since we want to hook into WordPress after the wporg_setup_slider callback function was registered (functions.php executed) our best chance would be the after_setup_theme hook.
function wporg_disable_slider() {
// Make sure all parameters match the add_action() call exactly.
remove_action( 'template_redirect', 'wporg_setup_slider', 9 );
}
// Make sure we call remove_action() after add_action() has been called.
add_action( 'after_setup_theme', 'wporg_disable_slider' );
h2 Removing All Callbacks
You can also remove all of the callback functions associated with a hook by using remove_all_actions() / remove_all_filters().
h2 Determining the Current Hook
Sometimes you want to run an Action or a Filter on multiple hooks, but behave differently based on which one is currently calling it.
You can use the current_action() / current_filter() to determine the current Action / Filter.
function wporg_modify_content( $content ) {
switch ( current_filter() ) {
case 'the_content':
// Do something.
break;
case 'the_excerpt':
// Do something.
break;
}
return $content;
}
add_filter( 'the_content', 'wporg_modify_content' );
add_filter( 'the_excerpt', 'wporg_modify_content' );
h2 Checking How Many Times a Hook Has Run
function wporg_custom() {
// If save_post has been run more than once, skip the rest of the code.
if ( did_action( 'save_post' ) !== 1 ) {
return;
}
// ...
}
add_action( 'save_post', 'wporg_custom' );
h2 officeal link : https://developer.wordpress.org/plugins/hooks/advanced-topics/