I am using a global variable that is defining in a function runs in after_setup_theme
. This variable is not getting update changes in Theme Customizer.
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";
}
}
This code is adding an example setting in Site Identity section in Theme Customizer which is not working. If i change;
add_action("after_setup_theme", "example_after_setup_theme");
to
add_action("wp", "example_after_setup_theme");
Its working. But i need it in after_setup_theme
. Any ideas for solution?