Skip to main content
Bounty Ended with 50 reputation awarded by Ünsal Korkmaz
wrong action
Source Link

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("init""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 ];
                }
            }
        }
    }
}

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("init", "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 ];
                }
            }
        }
    }
}

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(
            '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 ];
                }
            }
        }
    }
}
Source Link

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("init", "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 ];
                }
            }
        }
    }
}