How to prevent double execution of do_action statements

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.

Read More

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


}

Related posts

Leave a Reply

2 comments

  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. 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.