The maybe_hash_hex_color()
WordPress PHP function is used to ensure that any hex color is properly hashed. This function is useful when working with colors in your WordPress theme or plugin development.
Usage
The maybe_hash_hex_color()
function takes one required parameter, which is the hex color code that needs to be checked and possibly hashed. The function will return the input string with a hash added to the beginning, if the input string is a valid hex color code without a hash. If the input string is not a valid hex color code, the function will return the value untouched.
Here is an example of how to use maybe_hash_hex_color()
function:
$color = '00ff00'; // This is a valid hex color code without a hash $hashed_color = maybe_hash_hex_color( $color ); echo $hashed_color; // Output: #00ff00
In the above example, the input $color
is a valid hex color code without a hash. The maybe_hash_hex_color()
function adds a hash to the beginning of the input string and returns it in the $hashed_color
variable. The output of the function call is #00ff00
.
Parameters
$color
(string) – Required. The hex color code to be checked and possibly hashed.
More information
See WordPress Developer Resources: maybe_hash_hex_color()
This function was introduced in WordPress 4.7.0 and is not deprecated. The function code can be found in the wp-includes/formatting.php
file. Related functions include sanitize_hex_color_no_hash()
and sanitize_hex_color()
.
Examples
Example 1
In this example, we will use the maybe_hash_hex_color()
function to ensure that the input hex color code has a hash.
$color = 'ff0000'; // This is a valid hex color code without a hash $hashed_color = maybe_hash_hex_color( $color ); echo $hashed_color; // Output: #ff0000
The above example will output #ff0000
. The input $color
is a valid hex color code without a hash. The maybe_hash_hex_color()
function adds a hash to the beginning of the input string and returns it in the $hashed_color
variable.
Example 2
In this example, we will use the maybe_hash_hex_color()
function to check an input hex color code that already has a hash.
$color = '#0000ff'; // This is a valid hex color code with a hash $hashed_color = maybe_hash_hex_color( $color ); echo $hashed_color; // Output: #0000ff
The above example will output #0000ff
. The input $color
is a valid hex color code with a hash. The maybe_hash_hex_color()
function returns the input string untouched, because it is already properly formatted.
Example 3
In this example, we will use the maybe_hash_hex_color()
function to check an invalid input hex color code.
$color = '12345'; // This is not a valid hex color code $hashed_color = maybe_hash_hex_color( $color ); echo $hashed_color; // Output: 12345
The above example will output 12345
. The input $color
is not a valid hex color code, so the maybe_hash_hex_color()
function returns the input string untouched.
Example 4
In this example, we will use the maybe_hash_hex_color()
function to check an input hex color code that includes