After_Setup_Theme、全局变量和主题定制器

时间:2015-10-17 作者:Ünsal Korkmaz

我使用的是在函数中定义的全局变量after_setup_theme. 此变量未在主题自定义程序中获取更新更改。

Let me explain this with an example:

add_action( \'customize_register\', "example_customizer_register");
function example_customizer_register($wp_customize) {

        $wp_customize->add_setting( \'example_settings[example-variable]\', array(
            \'type\'      => \'option\',
            \'default\'   => false,
            \'sanitize_callback\' => \'esc_attr\'
        ) );
        $wp_customize->add_control( \'example_settings[example-variable]\', array(
            \'label\'    => \'Example Setting\',
            \'type\' => \'checkbox\',
            \'section\'  => \'title_tagline\',
        ) );    
}

add_action("after_setup_theme", "example_after_setup_theme");
function example_after_setup_theme(){
    global $example_settings;
    $example_settings = get_option( "example_settings", array());
}


add_action("wp_head", "example_wp_head");
function example_wp_head(){
    global $example_settings;
    if (isset($example_settings["example-variable"]) && true == $example_settings["example-variable"]) {
        echo "Example Setting";
    }   
}
此代码正在主题自定义程序的站点标识部分添加一个示例设置,但该设置不起作用。如果我改变;

add_action("after_setup_theme", "example_after_setup_theme");

add_action("wp", "example_after_setup_theme");
它正在工作。但我需要它after_setup_theme. 有什么解决方案吗?

2 个回复
最合适的回答,由SO网友:Thomas Bensmann 整理而成

问题是WP customizer提交了更改,但当时尚未处理这些更改。如果您不能等待WP customizer使用稍后的操作来完成这项工作,那么这里有一个解决方案,您可以获取自定义信息并使用它覆盖我们拥有的信息。

add_action("after_setup_theme", "example_after_setup_theme");
function example_after_setup_theme(){
    global $example_settings;

    $option_key = "example_settings";
    $example_settings = get_option( $option_key, array() );

    // Check if wp_customize was posted
    if( isset( $_POST[\'wp_customize\'] ) && $_POST[\'wp_customize\'] == "on" && !empty( $_POST[\'customized\'] ) ){

        // All the variables we need to look for
        $variables_to_find = array(
            \'example-variable\'
        );

        // Get the customized data
        $customized = json_decode( stripslashes( $_POST[\'customized\'] ), true );

        // Make sure it\'s a proper array
        if( !empty( $customized ) && is_array( $customized ) ){

            // Lopp the customized items
            foreach ($variables_to_find as $sub_key) {

                // The key in the settings array
                $key = "{$option_key}[{$sub_key}]";

                // If a different value was posted
                if( array_key_exists( $key, $customized) ){

                    // Replace it in the current object with the one submitted
                    $example_settings[ $sub_key ] = $customized[ $key ];
                }
            }
        }
    }
}

SO网友:Michael Ecklund

如果你看WordPress Codex :: Actions Run During a Typical Request, 你会注意到动作挂钩after_setup_theme 比动作挂钩的发射时间要早得多wp.

这是什么意思$example_settings动作钩内部wp, 那就意味着$example_settings 在的操作挂钩之前已定义wp 已被解雇如果无法访问的全局变量$example_settingsafter_setup_theme,那就是动作钩after_setup_theme$example_settings 变量已定义。基本上,您的变量还不存在定义$example_settings 在钩子里after_setup_theme 或者在鱼钩开火之前。

相关推荐

RESTORE_CURRENT_BLOG()与MULTIME SWITCH_TO_BLOG(),然后删除$GLOBALS[‘_wp_Switched_Stack’]

与相关this answer, 哪些国家每次switch_to_blog() 你need 调用restore_current_blog() 否则,WP会认为它处于“切换”模式,并可能返回不正确的数据。我已经做了一些测试,可以确认这是一个问题。(它还回答了我在工作中遇到的一个关于文件上传URL的问题,对此我竖起大拇指。)在我的测试(详情如下)中,我发现有两种方法可以继续:始终配对switch_to_blog() 具有restore_current_blog()</使用switch_to_blog(),