rest_sanitize_boolean() WordPress Function

The rest_sanitize_boolean() function is used to sanitize a boolean value. This function will return true if the value is 1, 'true', 'TRUE', or '1', and false otherwise. This function is useful for validating user input.

rest_sanitize_boolean( bool|string|int $value ) #

Changes a boolean-like value into the proper boolean value.


Parameters

$value

(bool|string|int)(Required)The value being evaluated.


Top ↑

Return

(bool) Returns the proper associated boolean value.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_sanitize_boolean( $value ) {
	// String values are translated to `true`; make sure 'false' is false.
	if ( is_string( $value ) ) {
		$value = strtolower( $value );
		if ( in_array( $value, array( 'false', '0' ), true ) ) {
			$value = false;
		}
	}

	// Everything else will map nicely to boolean.
	return (bool) $value;
}


Top ↑

Changelog

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