WP_REST_Settings_Controller::set_additional_properties_to_false() WordPress Method

The `WP_REST_Settings_Controller::set_additional_properties_to_false()` method is used to prevent additional properties from being added to a setting when it is updated. This is useful when you want to ensure that only the properties that were explicitly set are included in the updated setting.

WP_REST_Settings_Controller::set_additional_properties_to_false( array $schema ) #

Recursively add additionalProperties = false to all objects in a schema.


Description

This is need to restrict properties of objects in settings values to only registered items, as the REST API will allow additional properties by default.


Top ↑

Parameters

$schema

(array)(Required)The schema array.


Top ↑

Return

(array)


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

	protected function set_additional_properties_to_false( $schema ) {
		switch ( $schema['type'] ) {
			case 'object':
				foreach ( $schema['properties'] as $key => $child_schema ) {
					$schema['properties'][ $key ] = $this->set_additional_properties_to_false( $child_schema );
				}

				$schema['additionalProperties'] = false;
				break;
			case 'array':
				$schema['items'] = $this->set_additional_properties_to_false( $schema['items'] );
				break;
		}

		return $schema;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.9.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.