WP_Widget_Media::update() WordPress Method

The WP_Widget_Media::update() method is used to update the widget settings from the admin dashboard. This is where you can specify which media file to display, and configure other widget settings.

WP_Widget_Media::update( array $new_instance, array $old_instance ) #

Sanitizes the widget form values as they are saved.


Description

Top ↑

See also


Top ↑

Parameters

$new_instance

(array)(Required)Values just sent to be saved.

$old_instance

(array)(Required)Previously saved values from database.


Top ↑

Return

(array) Updated safe values to be saved.


Top ↑

Source

File: wp-includes/widgets/class-wp-widget-media.php

	public function update( $new_instance, $old_instance ) {

		$schema = $this->get_instance_schema();
		foreach ( $schema as $field => $field_schema ) {
			if ( ! array_key_exists( $field, $new_instance ) ) {
				continue;
			}
			$value = $new_instance[ $field ];

			/*
			 * Workaround for rest_validate_value_from_schema() due to the fact that
			 * rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true.
			 */
			if ( 'boolean' === $field_schema['type'] && '' === $value ) {
				$value = false;
			}

			if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) {
				continue;
			}

			$value = rest_sanitize_value_from_schema( $value, $field_schema );

			// @codeCoverageIgnoreStart
			if ( is_wp_error( $value ) ) {
				continue; // Handle case when rest_sanitize_value_from_schema() ever returns WP_Error as its phpdoc @return tag indicates.
			}

			// @codeCoverageIgnoreEnd
			if ( isset( $field_schema['sanitize_callback'] ) ) {
				$value = call_user_func( $field_schema['sanitize_callback'], $value );
			}
			if ( is_wp_error( $value ) ) {
				continue;
			}
			$old_instance[ $field ] = $value;
		}

		return $old_instance;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Renamed $instance to $old_instance to match parent class for PHP 8 named parameter support.
4.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.