rest_is_boolean() WordPress Function

The rest_is_boolean() function is used to check if a value is boolean or not. This function is useful in validating user input.

rest_is_boolean( bool|string $maybe_bool ) #

Determines if a given value is boolean-like.


Parameters

$maybe_bool

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


Top ↑

Return

(bool) True if a boolean, otherwise false.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_is_boolean( $maybe_bool ) {
	if ( is_bool( $maybe_bool ) ) {
		return true;
	}

	if ( is_string( $maybe_bool ) ) {
		$maybe_bool = strtolower( $maybe_bool );

		$valid_boolean_values = array(
			'false',
			'true',
			'0',
			'1',
		);

		return in_array( $maybe_bool, $valid_boolean_values, true );
	}

	if ( is_int( $maybe_bool ) ) {
		return in_array( $maybe_bool, array( 0, 1 ), true );
	}

	return false;
}


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