sanitize_hex_color( string $color )

Sanitizes a hex color.


Description 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 Parameters

$color

(string) (Required)


Top ↑

Return Return

(string|void)


Top ↑

Source 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

Changelog
Version Description
3.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Rinku Y

    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.

    $wp_customize->add_setting( 'accent_color', array(
      'default' => '#f72525',
      'sanitize_callback' => 'sanitize_hex_color',
    ) );
    

You must log in before being able to contribute a note or feedback.