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