sanitize_hex_color_no_hash

The timeline below displays how wordpress function sanitize_hex_color_no_hash has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Sanitizes a hex color without a hash. 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.
 *
 * @since 3.4.0
 *
 * @param string $color
 * @return string|null
 */
function sanitize_hex_color_no_hash($color)
{
    $color = ltrim($color, '#');
    if ('' === $color) {
        return '';
    }
    return sanitize_hex_color('#' . $color) ? $color : null;
}

WordPress Version: 4.1

/**
 * Sanitizes a hex color without a hash. 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.
 *
 * @since 3.4.0
 *
 * @param string $color
 * @return string|null
 */
function sanitize_hex_color_no_hash($color)
{
    $color = ltrim($color, '#');
    if ('' === $color) {
        return '';
    }
    return sanitize_hex_color('#' . $color) ? $color : null;
}

WordPress Version: 3.7

/**
 * Sanitizes a hex color without a hash. 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.
 *
 * @since 3.4.0
 * @uses sanitize_hex_color()
 *
 * @param string $color
 * @return string|null
 */
function sanitize_hex_color_no_hash($color)
{
    $color = ltrim($color, '#');
    if ('' === $color) {
        return '';
    }
    return sanitize_hex_color('#' . $color) ? $color : null;
}