rest_default_additional_properties_to_false() WordPress Function

The rest_default_additional_properties_to_false() function allows you to set the default value for the additional_properties parameter to false. This can be useful if you want to ensure that your REST API requests only return the data that you explicitly request.

rest_default_additional_properties_to_false( array $schema ) #

Sets the “additionalProperties” to false by default for all object definitions in the schema.


Parameters

$schema

(array)(Required)The schema to modify.


Top ↑

Return

(array) The modified schema.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_default_additional_properties_to_false( $schema ) {
	$type = (array) $schema['type'];

	if ( in_array( 'object', $type, true ) ) {
		if ( isset( $schema['properties'] ) ) {
			foreach ( $schema['properties'] as $key => $child_schema ) {
				$schema['properties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
			}
		}

		if ( isset( $schema['patternProperties'] ) ) {
			foreach ( $schema['patternProperties'] as $key => $child_schema ) {
				$schema['patternProperties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
			}
		}

		if ( ! isset( $schema['additionalProperties'] ) ) {
			$schema['additionalProperties'] = false;
		}
	}

	if ( in_array( 'array', $type, true ) ) {
		if ( isset( $schema['items'] ) ) {
			$schema['items'] = rest_default_additional_properties_to_false( $schema['items'] );
		}
	}

	return $schema;
}


Top ↑

Changelog

Changelog
VersionDescription
5.6.0Support the "patternProperties" keyword.
5.5.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.

Show More