Is there any way to prevent double execution of do_action statements? For example, I have the following lines:
do_action('myhook1', 'myfunction1');
do_action('myhook2', 'myfunction2');
do_action('myhook3', 'myfunction3');
There are also other plugins that “might” be executing them.
Is there a built-in WordPress function or some means to prevent dual execution of do_action statements? What I need is that once they are activated or executed? WordPress will simply ignore the other do_action statements.
Something like this condition:
if (!(already_executed)) {
//not executed
//execute the do_action lines
do_action('myhook1', 'myfunction1');
do_action('myhook2', 'myfunction2');
do_action('myhook3', 'myfunction3');
}
To check if someone already hooked into
myhook1
If you want to check if the function
myfunction1
was already called (maybe by a simple function call), you have to set a ‘marker’Before you call
do_action()
, you can check if the function was already calledThere are two ways:
Pass all the parameters at once:
Create custom actions with the parameter as part of the name (if it is a scalar):
Note the second parameter of
do_action()
is not a function, it is a string. So, when somebody else is using that hook withadd_action()
no function will be called twice unless you are using exact same hook two times on a page.