Discussion:
Hook trouble
Dobri
2013-09-11 15:43:55 UTC
Permalink
Hi,

I'm working on extending a badly designed plugin. To make it short, the plugin has a hook onto which it attaches its own function like so:

add_action('hook_name', array(&$class_reference, 'function_name'), 10, 3);

I override that with my own function set like so:

function maybe_override_function($field, $display, $values)
{
if( $condition )
{
global $class_reference;
remove_action('hook_name', array(&$class_reference, 'function_name'), 10);
add_action('hook_name', 'restore_function', 11);
}
}

function restore_function()
{
global $class_reference;
add_action('hook_name', array(&$class_reference, 'function_name'), 10, 3);
}

add_action('hook_name', 'maybe_override_function', 9, 3);

Basically, I want to not touch the function if a condition is not met but also remove it if it's not met *and* set it up so it runs ok next time too. In my mind, if my function runs at priority 9, adds a restoring function with priority 11 and then that restoring function re-adds the function at priority 10, the original function which I'm trying to restore will not run. However, it does. Essentially the order is maybe_override_function ==> restore_function ==> $class_reference->function_name. Is that normal behavior? If so, what would be the way to do this? I need to restore the regular 10 hook so it behaves ok next time it's called. Thanks for any help

P.S. the original function *is* removed and doesn't run if I remove the add_action('hook_name', 'restore_function', 11); altogether but then it is never restored either so next time the hook is called and condition is not met, nothing is rendered.
P.S.(2) obviously those are obscured names, my functions/variables/etc are within a class and named much better.

~Dobri
J.D. Grimes
2013-09-11 15:48:37 UTC
Permalink
Post by Dobri
In my mind, if my function runs at priority 9, adds a restoring function with priority 11 and then that restoring function re-adds the function at priority 10, the original function which I'm trying to restore will not run. However, it does.
If I remember correctly, functions added before the hook has finished running, but after their priority has already run, will be run last. Its odd I know. I think there might be a ticket for that, not sure though.
J.D. Grimes
2013-09-11 15:50:16 UTC
Permalink
Oh, and a solution in your situation would be to add the hook in the maybe_override_function(), if the condition isn't met.
Post by J.D. Grimes
Post by Dobri
In my mind, if my function runs at priority 9, adds a restoring function with priority 11 and then that restoring function re-adds the function at priority 10, the original function which I'm trying to restore will not run. However, it does.
If I remember correctly, functions added before the hook has finished running, but after their priority has already run, will be run last. Its odd I know. I think there might be a ticket for that, not sure though.
Dobri
2013-09-11 16:38:56 UTC
Permalink
… duh

Thanks for that, that's exactly what I ended up doing.

~Dobri
Post by J.D. Grimes
Oh, and a solution in your situation would be to add the hook in the maybe_override_function(), if the condition isn't met.
Post by J.D. Grimes
Post by Dobri
In my mind, if my function runs at priority 9, adds a restoring function with priority 11 and then that restoring function re-adds the function at priority 10, the original function which I'm trying to restore will not run. However, it does.
If I remember correctly, functions added before the hook has finished running, but after their priority has already run, will be run last. Its odd I know. I think there might be a ticket for that, not sure though.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...