sanitize_hex_color( string $color ): string|void
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
.
View all references
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;
}
}
Changelog
Version | Description |
---|---|
3.4.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
It is usually most important to set the default value of the setting as well as its sanitization callback, which will ensure that no unsafe data is stored in the database.
As of WordPress 4.6 this function is globally available, see https://core.trac.wordpress.org/ticket/27583