apply_filters( ‘widget_update_callback’, array $instance, array $new_instance, array $old_instance, WP_Widget $widget )

Filters a widget’s settings before saving.

Description

Returning false will effectively short-circuit the widget’s ability to update settings.

Parameters

$instancearray
The current widget instance’s settings.
$new_instancearray
Array of new widget settings.
$old_instancearray
Array of old widget settings.
$widgetWP_Widget
The current widget instance.

Source

$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    You may filter any field that already registered. For testing, Firstly we create a custom widget field end of the each widgets control form.

    if ( ! function_exists( 'wpdocs_display_custom_field_in_widget_form' ) ) {
    
        add_action( 'in_widget_form', 'wpdocs_display_custom_field_in_widget_form', 10, 3 );
    
        /**
         * Append custom field end of the widgets control form
         * Fires at the end of the widget control form.
         */
        function wpdocs_display_custom_field_in_widget_form( $widget, $return, $instance ) {
            $column = isset( $instance['column'] ) ? $instance['column'] : '';
    
            ob_start(); 
            ?>
            
                <label for="<?php echo esc_attr( get_field_id( 'itclan_bs_grid_class' ) ) ?>">
                    <input class="widefat" value="" id="<?php echo esc_attr( get_field_id( 'column' ) ) ?>" name="<?php echo esc_attr( get_field_name( 'column' ) ) ?>" type="text" />
                </label>
    
            <?php 
            echo ob_get_clean();
        }
    }

    Now, we filter widget’s settings before saving. It will save our custom field column data.

    if ( ! function_exists( 'wpdocs_update_custom_field_in_widget_form' ) ) {
    
        add_action( 'widget_update_callback', 'wpdocs_update_custom_field_in_widget_form', 10, 2 );
    
        /**
         * Update widget fields
         * Filters a widget’s settings before saving.
         */
        function wpdocs_update_custom_field_in_widget_form( $instance, $new_instance ) {
            $instance['column'] = ! empty( $new_instance['column'] ) ? $new_instance['column'] : '';
            return $instance;
        }
    }

You must log in before being able to contribute a note or feedback.