sanitize_hex_color()
Sanitizes a hex color.
Returns either '', a 3 or 6 digit hex color (with #), or nothing. For sanitizing values without a #, see sanitize_hex_color_no_hash().
No Hooks.
Returns
String|null.
Usage
sanitize_hex_color( $color );
- $color(string) (required)
- .
Changelog
| Since 3.4.0 | Introduced. |
sanitize_hex_color() sanitize hex color code WP 6.8.3
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;
}
}