maybe_hash_hex_color()
云策文档标注
概述
maybe_hash_hex_color() 是一个 WordPress 函数,用于确保十六进制颜色值正确添加哈希前缀(#)。它主要与 sanitize_hex_color_no_hash() 配合使用,处理颜色字符串的标准化。
关键要点
- 函数功能:为有效的十六进制颜色值添加 # 前缀,否则返回原值。
- 参数:$colorstring(必需),可以是带或不带 # 的颜色值。
- 返回值:如果是有效十六进制颜色,返回带 # 的字符串;否则返回原始值。
- 依赖关系:内部调用 sanitize_hex_color_no_hash() 进行验证。
- 使用场景:适用于需要确保颜色格式一致性的开发场景,如自定义背景处理。
- 版本历史:自 WordPress 3.4.0 引入。
代码示例
function maybe_hash_hex_color( $color ) {
$unhashed = sanitize_hex_color_no_hash( $color );
if ( $unhashed ) {
return '#' . $unhashed;
}
return $color;
}
原文内容
Ensures that any hex color is properly hashed.
Description
Otherwise, returns value untouched.
This method should only be necessary if using sanitize_hex_color_no_hash() .
Parameters
$colorstringrequired-
The color value to add the hash prefix to. Can be with or without a #.
Source
function maybe_hash_hex_color( $color ) {
$unhashed = sanitize_hex_color_no_hash( $color );
if ( $unhashed ) {
return '#' . $unhashed;
}
return $color;
}
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |