I'm trying to remove a parent theme's add_action call to a pluggable function, but can't get it to work by calling remove_action()
- I have to redeclare the function and just leave it empty. Is this normal? I'd think I could use remove_action
to simply never call the function.
Here's the parent theme code:
add_action( 'tha_content_while_before', 'fwp_archive_header' );
if ( !function_exists('fwp_archive_header') ) {
function fwp_archive_header() {
// do stuff
}
}
And in the child theme (NOT working):
add_action( 'init', 'remove_parent_actions_filters' );
function remove_parent_actions_filters() {
remove_action( 'tha_content_while_before', 'fwp_archive_header' );
}
I have also tried swapping out 'init
' for 'after_setup_theme
' and 'wp_loaded
'; I've also tried lowering and raising the priority, nothing worked. The only thing that did work was this:
In the child theme (working):
function fwp_archive_header() {
// do nothing
}
Can that be right, I have to redeclare the function to get rid of it?
Thanks!
I'm trying to remove a parent theme's add_action call to a pluggable function, but can't get it to work by calling remove_action()
- I have to redeclare the function and just leave it empty. Is this normal? I'd think I could use remove_action
to simply never call the function.
Here's the parent theme code:
add_action( 'tha_content_while_before', 'fwp_archive_header' );
if ( !function_exists('fwp_archive_header') ) {
function fwp_archive_header() {
// do stuff
}
}
And in the child theme (NOT working):
add_action( 'init', 'remove_parent_actions_filters' );
function remove_parent_actions_filters() {
remove_action( 'tha_content_while_before', 'fwp_archive_header' );
}
I have also tried swapping out 'init
' for 'after_setup_theme
' and 'wp_loaded
'; I've also tried lowering and raising the priority, nothing worked. The only thing that did work was this:
In the child theme (working):
function fwp_archive_header() {
// do nothing
}
Can that be right, I have to redeclare the function to get rid of it?
Thanks!
Share Improve this question edited Jul 29, 2020 at 0:18 Michelle asked Jul 28, 2020 at 20:31 MichelleMichelle 3,4734 gold badges26 silver badges34 bronze badges 2 |2 Answers
Reset to default 3The parent theme's functions.php runs after the child theme's, so in order to remove an action defined by the parent theme the remove_action
call must be delayed using a hook after the parent theme registers the action. So putting the remove_action
call purely inside the child's functions.php won't work. It must be attached to a hook.
However from the code excerpt in the question it is not clear if the parent's add_action( 'tha_content_while_before', 'fwp_archive_header' );
line is just in functions.php or is that inside an action? If it is inside an action, hook the remove_action
call to the same action but with bigger priority (so it runs after). Note I'm talking about the action the parent add_action
call is inside. Something like this:
add_action( 'some_hook', 'some_parent_function', 10 );
function some_parent_function() {
/* ...some code... */
add_action( 'tha_content_while_before', 'fwp_archive_header' );
if ( !function_exists('fwp_archive_header') ) {
function fwp_archive_header() {
// do stuff
}
}
/* ...some code... */
}
The remove would look like:
add_action( 'some_hook', 'remove_parent_actions_filters', 11 );
function remove_parent_actions_filters() {
remove_action( 'tha_content_while_before', 'fwp_archive_header' );
}
Another rule of thumb attempt is to hook your remove call to wp_loaded
, e.g.: add_action( 'wp_loaded', 'remove_parent_actions_filters', 1000 );
. Should be still on time to affect how pages are rendered, but would likely be late enough to override most common parent theme hooks.
On the other hand declaring an empty function with the name fwp_archive_header
is almost as good solution. Note though it is not “redeclare”, as there's no such thing in PHP. It's more like “predeclare”, before the parent theme, so the parent theme won't declare it's own function with the same name (with appropriate checks in place).
Inside your remove_parent_actions_filters() function, add a test to see if the parent theme's function has been loaded. Maybe you are calling your hook too soon.
add_action( 'init', 'remove_parent_actions_filters' );
function remove_parent_actions_filters() {
if (!function_exists('fwp_archive_header')) {wp_die("fwp_archive_header function is not loaded");}
remove_action( 'tha_content_while_before', 'fwp_archive_header' );
}
Added
Try using the after_setup_theme hook instead. See https://codex.wordpress/Plugin_API/Action_Reference/after_setup_theme .
remove_action
outside of the 'remove' function? On its own in your child theme > functions.php ? Just guessing...remove_action
can be tough. – shanebp Commented Jul 28, 2020 at 20:42