检查是否有人已经myhook1
if( 0 >= did_action( \'myhook1\' ) )
do_action( \'myhook1\', \'myfunction1\' );
如果要检查函数
myfunction1
已被调用(可能通过简单的函数调用),您必须设置“标记”
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 ...]
}
在你打电话之前
do_action()
, 您可以检查函数是否已被调用
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\' );