W3cubDocs

/WordPress

rest_parse_hex_color( string $color ): string|false

Parses a 3 or 6 digit hex color (with #).

Parameters

$colorstringrequired
3 or 6 digit hex color (with #).

Return

string|false Color value on success, false on failure.

Source

function rest_parse_hex_color( $color ) {
	$regex = '|^#([A-Fa-f0-9]{3}){1,2}$|';
	if ( ! preg_match( $regex, $color, $matches ) ) {
		return false;
	}

	return $color;
}

Changelog

Version Description
5.4.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_parse_hex_color