WP_Widget::update_callback( int $deprecated = 1 )

In this article

Handles changed settings (Do NOT override).

Parameters

$deprecatedintoptional
Not used.

Default:1

Source

public function update_callback( $deprecated = 1 ) {
	global $wp_registered_widgets;

	$all_instances = $this->get_settings();

	// We need to update the data.
	if ( $this->updated ) {
		return;
	}

	if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
		// Delete the settings for this instance of the widget.
		if ( isset( $_POST['the-widget-id'] ) ) {
			$del_id = $_POST['the-widget-id'];
		} else {
			return;
		}

		if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number'] ) ) {
			$number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];

			if ( $this->id_base . '-' . $number === $del_id ) {
				unset( $all_instances[ $number ] );
			}
		}
	} else {
		if ( isset( $_POST[ 'widget-' . $this->id_base ] ) && is_array( $_POST[ 'widget-' . $this->id_base ] ) ) {
			$settings = $_POST[ 'widget-' . $this->id_base ];
		} elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] === $this->id_base ) {
			$num      = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
			$settings = array( $num => array() );
		} else {
			return;
		}

		foreach ( $settings as $number => $new_instance ) {
			$new_instance = stripslashes_deep( $new_instance );
			$this->_set( $number );

			$old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();

			$was_cache_addition_suspended = wp_suspend_cache_addition();
			if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
				wp_suspend_cache_addition( true );
			}

			$instance = $this->update( $new_instance, $old_instance );

			if ( $this->is_preview() ) {
				wp_suspend_cache_addition( $was_cache_addition_suspended );
			}

			/**
			 * Filters a widget's settings before saving.
			 *
			 * Returning false will effectively short-circuit the widget's ability
			 * to update settings.
			 *
			 * @since 2.8.0
			 *
			 * @param array     $instance     The current widget instance's settings.
			 * @param array     $new_instance Array of new widget settings.
			 * @param array     $old_instance Array of old widget settings.
			 * @param WP_Widget $widget       The current widget instance.
			 */
			$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );

			if ( false !== $instance ) {
				$all_instances[ $number ] = $instance;
			}

			break; // Run only once.
		}
	}

	$this->save_settings( $all_instances );
	$this->updated = true;
}

Hooks

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

Filters a widget’s settings before saving.

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

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