[wp-hackers] Hook trouble

Dobri dyordan1 at ramapo.edu
Wed Sep 11 15:43:55 UTC 2013


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



More information about the wp-hackers mailing list