sanitize_hex_color_no_hash

Sanitizes a hex color without a hash.

Description

(string|null) sanitize_hex_color_no_hash( (string) $color ); 

Use sanitize_hex_color() when possible.

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.

Returns (string|null)

Parameters (1)

0. $color (string)
The color.

Usage

  1. if ( !function_exists( 'sanitize_hex_color_no_hash' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The color. 
  5. $color = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = sanitize_hex_color_no_hash($color); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function sanitize_hex_color_no_hash( $color ) { 
  2. $color = ltrim( $color, '#' ); 
  3.  
  4. if ( '' === $color ) { 
  5. return ''; 
  6.  
  7. return sanitize_hex_color( '#' . $color ) ? $color : null;