1

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');


}
1
  • Do you want to call a function with do_acton()? If so, you do it wrong. The syntax of do_action() is do_action( [tag], [argument [, argument, argument]] ). Maybe you mean add_action( [tag], [function to call] )
    – Ralf912
    Commented Jan 25, 2013 at 9:43

2 Answers 2

1

To check if someone already hooked into myhook1

if( 0 >= did_action( 'myhook1' ) )
  do_action( 'myhook1', 'myfunction1' );

If you want to check if the function myfunction1 was already called (maybe by a simple function call), you have to set a 'marker'

function myfunction1 ( $some_args ) {

// with a simple global define (a bad solution)
  define( 'MYFUNCTION1_DONE', TRUE );

// or with the globals array (also a bad solution)

  $myfunction_calls = isset( $GLOBALS['myfunction_calls'] ) ?
    $GLOBALS['myfunction_calls'] : array();
  if( ! is_array( $myfunction_calls ) )
    $myfunction_calls = array();

  $myfunction_calls[__FUNCTION__] = TRUE;
  $GLOBALS['myfunction_calls'] = $myfunction_calls;

// or with database
//
// you have to reset the database entry everytime the script ends
// this is can be done by add_action( 'shutdown', 'reset_myfunctioncalls_db_entry' );
// or in a class with a __destruct() method
  $myfunction_calls = get_option( 'myfunction_calls', TRUE );
  if( ! is_array( $myfunction_calls ) )
    $myfunction_calls = array();

  $myfunction_calls[__FUNCTION__] = TRUE;
  upodate_option( 'myfunction_calls', $myfunctions );

[... some other code ...]
}

Before you call do_action(), you can check if the function was already called

if( ! defined( 'MYFUNCTION1_DONE' ) || TRUE !== MYFUNCTION1_DONE )
  do_action( 'myhook1', 'myfunction1' );

// or
$myfunction_calls = isset( $GLOBALS['myfunction_calls'] ) ?
  $GLOBALS['myfunction_calls'] : array();

if( ! isset( $myfunction_calls['myfunction1'] ) || TRUE !== $myfunction_calls['myfunction1'] )
  do_action( 'myhook1', 'myfunction1' );

// or
$myfunction_calls = get_option( 'myfunction_calls', TRUE );

if( ! isset( $myfunction_calls['myfunction1'] ) || TRUE !== $myfunction_calls['myfunction1'] )
  do_action( 'myhook1', 'myfunction1' );
2
  • thanks Ralf, does Wordpress core ignore the other do_action if the same hook is already executed? For example this statement occurred twice do_action( mytag, myargument ) Commented Jan 25, 2013 at 10:29
  • If you have something like this do_action( 'foo', 'a string as argument' ); [...] do_action( 'foo', 'another string as argument' ); then the hook will be executed twice. Simply don't use the same tag (hook) more than once.
    – Ralf912
    Commented Jan 25, 2013 at 11:55
1

There are two ways:

  1. Pass all the parameters at once:

    do_action( 'myhook1', 'myfunction1', 'myfunction2', 'myfunction3' );
    // or
    do_action( 'myhook1', array ( 'myfunction1', 'myfunction2', 'myfunction3' ) );
    
  2. Create custom actions with the parameter as part of the name (if it is a scalar):

    do_action( 'myhook1_myfunction1' );
    do_action( 'myhook1_myfunction2' );
    do_action( 'myhook1_myfunction3' );
    

Note the second parameter of do_action() is not a function, it is a string. So, when somebody else is using that hook with add_action() no function will be called twice unless you are using exact same hook two times on a page.

3
  • If someone else hooked into myhook1, then the function(s) will be called( executed) a second time, or am I wrong? The question was how to prevent a second call of the functions, if I didn't missunderstood the question.
    – Ralf912
    Commented Jan 25, 2013 at 9:39
  • @Ralf912 No, the number of registered callbacks for a hook is almost unlimited. Using the same hook does not mean the same function will be executed.
    – fuxia
    Commented Jan 25, 2013 at 9:43
  • I think he mean add_action and not do_action, because 'myfunction1' is a bit wired argument.
    – Ralf912
    Commented Jan 25, 2013 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.