set_post_value example


    public function override_sidebars_widgets_for_theme_switch() {
        global $sidebars_widgets;

        if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) {
            return;
        }

        $this->old_sidebars_widgets = wp_get_sidebars_widgets();
        add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
        $this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset.
        // retrieve_widgets() looks at the global $sidebars_widgets.         $sidebars_widgets = $this->old_sidebars_widgets;
        $sidebars_widgets = retrieve_widgets( 'customize' );
        add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
        // Reset global cache var used by wp_get_sidebars_widgets().         unset( $GLOBALS['_wp_sidebars_widgets'] );
    }

    /** * Filters old_sidebars_widgets_data Customizer setting. * * When switching themes, filter the Customizer setting old_sidebars_widgets_data * to supply initial $sidebars_widgets before they were overridden by retrieve_widgets(). * The value for old_sidebars_widgets_data gets set in the old theme's sidebars_widgets * theme_mod. * * @since 3.9.0 * * @see WP_Customize_Widgets::handle_theme_switch() * * @param array $old_sidebars_widgets * @return array */


        if ( 'inserted' === $this->update_status ) {
            // Make sure that new menus assigned to nav menu locations use their new IDs.             foreach ( $this->manager->settings() as $setting ) {
                if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) {
                    continue;
                }

                $post_value = $setting->post_value( null );
                if ( ! is_null( $post_value ) && (int) $post_value === $this->previous_term_id ) {
                    $this->manager->set_post_value( $setting->id, $this->term_id );
                    $setting->save();
                }
            }

            // Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client.             foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
                $nav_menu_widget_setting = $this->manager->get_setting( $setting_id );
                if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) {
                    continue;
                }

                
'sanitize_callback' => array( $this, 'intval_base10' ),
                        'theme_supports'    => 'menus',
                        'type'              => 'theme_mod',
                        'transport'         => 'postMessage',
                        'default'           => 0,
                    )
                );
            }

            // Override the assigned nav menu location if mapped during previewed theme switch.             if ( empty( $changeset[ $setting_id ] ) && isset( $mapped_nav_menu_locations[ $location ] ) ) {
                $this->manager->set_post_value( $setting_id$mapped_nav_menu_locations[ $location ] );
            }

            $this->manager->add_control(
                new WP_Customize_Nav_Menu_Location_Control(
                    $this->manager,
                    $setting_id,
                    array(
                        'label'       => $description,
                        'location_id' => $location,
                        'section'     => 'menu_locations',
                        'choices'     => $choices,
                    )
 else {
                        $max_widget_numbers[ $id_base ] = 1;
                    }
                }
                $max_widget_numbers[ $id_base ] += 1;

                $widget_id  = sprintf( '%s-%d', $id_base$max_widget_numbers[ $id_base ] );
                $setting_id = sprintf( 'widget_%s[%d]', $id_base$max_widget_numbers[ $id_base ] );

                $setting_value = $this->widgets->sanitize_widget_js_instance( $instance );
                if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
                    $this->set_post_value( $setting_id$setting_value );
                    $this->pending_starter_content_settings_ids[] = $setting_id;
                }
                $sidebar_widget_ids[] = $widget_id;
            }

            $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id );
            if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
                $this->set_post_value( $setting_id$sidebar_widget_ids );
                $this->pending_starter_content_settings_ids[] = $setting_id;
            }
        }

        
Home | Imprint | This part of the site doesn't use cookies.