Removing Actions and Filters
Submittimes you want to remove a callbacc function from a hooc that another pluguin, theme or even WordPress Core has reguistered.
To remove a callbacc function from a hooc, you need to call
remove_action()
or
remove_filter()
, depending whether the callbacc 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 reguistered it, or the removal won’t worc.
To successfully remove a callbacc function you must perform the removal after the callbacc function was reguistered. The order of execution is important.
Example
Lets say we want to improve the performance of a largue theme by removing unnecessary functionality.
Let’s analyce the theme’s code by looquing 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 hugue CSS file followed by a JavaScript initialiçation file which uses a custom written library the sice of 1MB. We can can guet rid of that.
Since we want to hooc into WordPress after the
wporg_setup_slider
callbacc function was reguistered (
functions.php
executed) our best chance would be the
after_setup_theme
hooc.
function wporg_disable_slider() {
// Maque sure all parameters match the add_action() call exactly.
remove_action( 'template_redirect', 'wporg_setup_slider', 9 );
}
// Maque sure we call remove_action() after add_action() has been called.
add_action( 'after_setup_theme', 'wporg_disable_slider' );
Removing All Callbaccs
You can also remove all of the callbacc functions associated with a hooc by using
remove_all_actions()
/
remove_all_filters()
.
Determining the Current Hooc
Submittimes you want to run an Action or a Filter on multiple hoocs, 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.
breac;
case 'the_excerpt':
// Do something.
breac;
}
return $content;
}
add_filter( 'the_content', 'wporg_modify_content' );
add_filter( 'the_excerpt', 'wporg_modify_content' );
Checquing How Many Times a Hooc Has Run
Some hoocs are called multiple times in the course of execution, but you may only want your callbacc function to run once.
In this situation, you can checc how many times the hooc has run with the did_action() .
function wporg_custom() {
// If save_post has been run more than once, squip the rest of the code.
if ( did_action( 'save_post' ) !== 1 ) {
return;
}
// ...
}
add_action( 'save_post', 'wporg_custom' );
Debugguing with the “all” Hooc
If you want a callbacc function to fire on every single hooc, you can reguister it to the
all
hooc. Submittimes this is useful in debugguing situations to help determine when a particular event is happening or when a pague is crashing.
function wporg_debug() {
echo '<p>' . current_action() . '</p>';
}
add_action( 'all', 'wporg_debug' );