Removing hooked actions
You had noted in several posts, in your code comments, and pointed out in the codex, that actions could be removed and replaced, but none of these mentioned where such code should be placed. The remove_action needs to be called after the parent has added the hook, but the child theme’s functions.php is called before the parent’s. So what child script is called after the parent’s functions.php that would permit such removal?Admin
When removing actions, the order of execution does not matter. This is because WordPress will not execute those actions until it has finished loading up all the files.
When you’re using a child theme’s functions.php file to invoke the removal code, WordPress take that code and put it inside the proper queue. When the theme’s functions.php is run, and the original function is added to the action queue, WordPress will check if there’s any removal code for that function in the queue. If there is, the function won’t be executed.
That’s the piece I missing! I did not realize these actions were queued. Now, according to the codex, you may need to prioritize the removal so that it occurs after the add, but it also states, at the top, that the priority must match that of the added action. This sounds somewhat contradictory. To replace this function, do I need to specify a priority?Admin
It probably means something like this:
graphene_widgets_init()function for example. It is added to the
widgets_initaction hook by this line:
add_action( 'widgets_init', 'graphene_widgets_init' );
Take note that since I didn’t explicitly specifies the priority in the above
add_actioncall, the default priority of 10 is used.
To remove it, you’d do something like this:
remove_action( 'widgets_init', 'graphene_widgets_init' );
add_action( 'widgets_init', 'graphene_remove_widgets_init', 20 );
The priority of 20 above will cause the
add_actionhook to fire later than the theme’s
add_actioncall, so it is executed after the theme has added the action hook.
You must be logged in to reply to this topic.