rest_parse_hex_color()WP 5.4.0

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

No Hooks.

Return

String|false.

Usage

rest_parse_hex_color( $color );
$color(string) (required)
3 or 6 digit hex color (with #).

Changelog

Since 5.4.0 Introduced.

rest_parse_hex_color() code WP 6.5.2

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

	return $color;
}