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


add_action("wp", "example_after_setup_theme");

Its working. But i need it in after_setup_theme. Any ideas for solution?

  • If your question has been answered, could you please accept the solution. Thanks. – Michael Ecklund Jan 4 '17 at 20:16

The issue is that WP customizer submits the changes, but has not processed these yet at that point in time. If you cant wait for WP customizer to do it's thing by using a later action, here is a solution where you get the customized information and use it to overwrite the information we had.

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(

        // 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 ];
  • While its not perfect solution i hoped (variables have to be set), it gives right approach to problem. Thanks! – Ünsal Korkmaz Oct 25 '15 at 19:55

If you look at WordPress Codex :: Actions Run During a Typical Request, you will notice that the action hook after_setup_theme is fired significantly earlier on than that of the action hook wp.

What does this mean?

  1. If you're able to access the global variable of $example_settings inside the action hook wp, then that means $example_settings has already been defined BEFORE the action hook of wp has been fired.
  2. If you're NOT able to access the global variable of $example_settings inside the action hook of after_setup_theme, then that means the action hook after_setup_theme is fired BEFORE the $example_settings variable has been defined. Basically, your variable doesn't exist yet.

How to fix this?

Define $example_settings in the hook after_setup_theme or before that hook is fired.

  • Well yeah.. Thats the question. Thanks for explaining the question more but its not the answer. A working solution will be appreciated – Ünsal Korkmaz Oct 21 '15 at 3:39
  • 1
    @ÜnsalKorkmaz I think this is the correct answer and I think you have not understood what Michael has explained. Note that actions is like events that happen one after one. If you are in a earlier event you can not access to data defined in a later events. Do you get it? – cybmeta Oct 24 '15 at 10:48
  • Yeah i got it. But this is a specific problem that has possibility to try it in a different way which i couldnt find and i hope someone can point it out. – Ünsal Korkmaz Oct 24 '15 at 12:21
  • Then I don't understand your problem :) – cybmeta Oct 25 '15 at 17:49
  • Check the solution. Thats what i am talking about. Different way. Thanks for your time – Ünsal Korkmaz Oct 25 '15 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.