add_action()中的函数参数到init

时间:2013-09-08 01:26:13

标签: php wordpress

我希望将函数中的参数传递给add_action(),但显然我需要通过do_action执行此操作。我的问题是add_action标记是'init',我认为如果我再次/过早地运行它会导致问题。有什么想法吗?

function taxonomy_equipment( ) {
    $name = "Equipment";
    $labels = array(
        'name'              => _x( $name, 'taxonomy general name' ),
        'singular_name'     => _x( $name, 'taxonomy singular name' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( str_replace(' ', '', strtolower($name)), 'exercise', $args );
}
add_action( 'init', 'taxonomy_equipment', 0 );

我想传递$name作为参数。

1 个答案:

答案 0 :(得分:2)

测试:

global $my_var;
$my_var = $value;
add_action('any_hook', 'myfunction');

function myfunction() {
    global $my_var;
}

<强>已更新

其他方式:

function taxonomy_equipment( ) {
    $my_arg_globals = $GLOBALS['my_arg_globals'];
    $name = $my_arg_globals['name'];
    $labels = array(
        'name'              => _x( $name, 'taxonomy general name' ),
        'singular_name'     => _x( $name, 'taxonomy singular name' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( str_replace(' ', '', strtolower($name)), 'exercise', $args );
}
$my_arg_globals = array('name' => 'Equipment');
add_action( 'init', 'taxonomy_equipment', 0 );

在这种情况下,工作非常简单:

function get_var_test()
{
    $my_arg_globals = $GLOBALS['my_arg_globals'];
    return $my_arg_globals['name'];
}
$my_arg_globals = array('name' => 'name1');
echo Sum();

$my_arg_globals = array('name' => 'name2');
echo Sum();

$my_arg_globals = array('name' => 'name3');
echo Sum();