0

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!

2
  • Have you tried placing the remove_action outside of the 'remove' function? On its own in your child theme > functions.php ? Just guessing... remove_action can be tough. Commented Jul 28, 2020 at 20:42
  • @shanebp Yes it doesn't do anything when moved outside the function. I believe to override the parent theme call it has to be hooked into either init or after_theme_setup. Commented Jul 28, 2020 at 21:05

2 Answers 2

3

The 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).

2
  • Thanks! Understood. The parent theme's add_action( 'tha_content_while_before', 'fwp_archive_header' ); and associated function is in a functions.php file, not wrapped in another action/function. I tried wp_loaded (and init, and after_setup_theme) with 1000 priority and nothing works. Super strange. Guess I'll just redeclare the function as empty. Thanks! Commented Jul 29, 2020 at 0:17
  • Is this theme available to examine? Taking a look at the code or actually checking why the remove action call fails using a debugger would be the go to next step in finding the issue! Commented Jul 29, 2020 at 14:56
1

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.org/Plugin_API/Action_Reference/after_setup_theme .

4
  • That definitely is an issue - I get the message from wp_die when I use your code. I've tried changing the priority to something really high like 1000 add_action( 'init', 'remove_parent_actions_filters',1000 ); but I still get the message. Stumped! Commented Jul 28, 2020 at 21:05
  • What you need to use is the hook that occurs after the parent style is loaded. Or maybe after all style files are loaded. Like the 'after_setup_theme' hook: codex.wordpress.org/Plugin_API/Action_Reference/… . Commented Jul 29, 2020 at 0:03
  • I tried that, didn't work. Commented Jul 29, 2020 at 0:09
  • Is it possible that the function you are trying to replace gets loaded later on (after theme setup)? Maybe this will help: codex.wordpress.org/Plugin_API/Action_Reference . And this might be a great resource (answers) wordpress.stackexchange.com/questions/162862/… . Commented Jul 30, 2020 at 1:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.