Title: maybe_hash_hex_color
Published: April 25, 2014
Last modified: August 20, 2026

---

# maybe_hash_hex_color( string $color ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#changelog)

Ensures that any hex color is properly hashed.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#description)󠁿

Otherwise, returns value untouched.

This method should only be necessary if using [sanitize_hex_color_no_hash()](https://developer.wordpress.org/reference/functions/sanitize_hex_color_no_hash/).

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#parameters)󠁿

 `$color`stringrequired

The color value to add the hash prefix to. Can be with or without a #.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#return)󠁿

 string The color with the hash prefix if it’s a valid hex color, otherwise the 
original value.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#source)󠁿

    ```php
    function maybe_hash_hex_color( $color ) {
    	$unhashed = sanitize_hex_color_no_hash( $color );
    	if ( $unhashed ) {
    		return '#' . $unhashed;
    	}

    	return $color;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/formatting.php#L6388)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/formatting.php#L6388-L6395)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#related)󠁿

| Uses | Description | 
| [sanitize_hex_color_no_hash()](https://developer.wordpress.org/reference/functions/sanitize_hex_color_no_hash/)`wp-includes/formatting.php` |

Sanitizes a hex color without a hash. Use [sanitize_hex_color()](https://developer.wordpress.org/reference/functions/sanitize_hex_color/) when possible.

  |

| Used by | Description | 
| [Custom_Background::admin_page()](https://developer.wordpress.org/reference/classes/custom_background/admin_page/)`wp-admin/includes/class-custom-background.php` |

Displays the custom background page.

  | 
| [_custom_background_cb()](https://developer.wordpress.org/reference/functions/_custom_background_cb/)`wp-includes/theme.php` |

Default custom background callback.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/maybe_hash_hex_color/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fmaybe_hash_hex_color%2F)
before being able to contribute a note or feedback.