I have a purchased theme that procedurally loads an action like this:
function func() {
echo "head content";
}
add_action( 'wp_head', 'func', 5 );
Following the remove action codex I tried the following:
remove_action( 'wp_head', 'func', 5 );
Which did not work so I figured it was a priority issue so I adjusted both higher and lower neither worked. Then I tried wrapping the function in another function like:
function testfunc() {
if (remove_action( 'wp_head', 'func', 5 )) {
echo "removed action";
}
}
add_action( 'wp_head', 'testfunc', 1000 );
Again playing with the priority. The whole time remove_action
is returning true but not removing the action. Then I tried adding the wrapping function in the init
action and it worked. Great! Unfortunately I have no idea why. Somewhat more perplexing is the wrapping function is required. Lastly, the codex specifically states:
It is also worth noting that you may need to prioritise the removal of the action to a hook that occurs after the action is added. You cannot successfully remove the action before it has been added.
And according to this codex entry both init
and get_header
(which worked) are before the wp_head
action the function was hooked into. Can someone demystify this for me?
Edit: To answer @sumit's question the add_action
happens via an include in the main themes functions.php (it includes library/core.php). The remove_action
is in the functions.php of the child theme.
add_action
andremove_action
?