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().


Top ↑

Parameters

$color

(string)(Required)


Top ↑

Return

(string|void)


Top ↑

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;
	}
}


Top ↑

Changelog

Changelog
VersionDescription
3.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More