sanitize_hex_color_no_hash() WordPress Function

The sanitize_hex_color_no_hash() function is used to sanitize a color value in hexadecimal format without a leading hash sign (#). This function is useful when working with color values that may or may not have a hash sign.

sanitize_hex_color_no_hash( string $color ) #

Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.


Description

Saving hex colors without a hash puts the burden of adding the hash on the UI, which makes it difficult to use or upgrade to other color types such as rgba, hsl, rgb, and HTML color names.

Returns either ”, a 3 or 6 digit hex color (without a #), or null.


Top ↑

Parameters

$color

(string)(Required)


Top ↑

Return

(string|null)


Top ↑

Source

File: wp-includes/formatting.php

function sanitize_hex_color_no_hash( $color ) {
	$color = ltrim( $color, '#' );

	if ( '' === $color ) {
		return '';
	}

	return sanitize_hex_color( '#' . $color ) ? $color : null;
}


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