wp_validate_boolean( mixed $var )

Filter/validate a variable as a boolean.


Description Description

Alternative to filter_var( $var, FILTER_VALIDATE_BOOLEAN ).


Top ↑

Parameters Parameters

$var

(mixed) (Required) Boolean value to validate.


Top ↑

Return Return

(bool) Whether the value is validated.


Top ↑

Source Source

File: wp-includes/functions.php

function wp_validate_boolean( $var ) {
	if ( is_bool( $var ) ) {
		return $var;
	}

	if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
		return false;
	}

	return (bool) $var;
}


Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Daniel Pietrasik

    wp_validate_boolean() tests:

    wp_validate_boolean(true)); //returns bool(true)
    wp_validate_boolean(false)); //returns bool(false)
    wp_validate_boolean('true')); //returns bool(true)
    wp_validate_boolean('false')); //returns bool(false)
    wp_validate_boolean('test')); //returns bool(true)
    wp_validate_boolean(123)); //returns bool(true)
    wp_validate_boolean(0)); //returns bool(false)
    wp_validate_boolean(null)); //returns bool(false)
    wp_validate_boolean([]); //returns bool(false)
    wp_validate_boolean(['test']); //returns bool(true)
    wp_validate_boolean([123]); //returns bool(true)
    wp_validate_boolean(new StdClass()) //returns bool(true)
    wp_validate_boolean()); //returns Error

You must log in before being able to contribute a note or feedback.