sanitize_hex_color() WordPress Function
The sanitize_hex_color() function is used to sanitize a hexadecimal color value. The function checks if the given value is a valid hex color code and if so, return the sanitized value. If the given value is not a valid hex color code, the function will return an empty string.
sanitize_hex_color( string $color ) #
Sanitizes a hex color.
Description
Returns either ”, a 3 or 6 digit hex color (with #), or nothing. For sanitizing values without a #, see sanitize_hex_color_no_hash().
Parameters
- $color
(string)(Required)
Return
(string|void)
Source
File: wp-includes/formatting.php
function sanitize_hex_color( $color ) {
if ( '' === $color ) {
return '';
}
// 3 or 6 hex digits, or the empty string.
if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
return $color;
}
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |