sanitize_hex_color() - WordPress 函数参考中文文档

返回介绍

sanitize_hex_color()

发布于 2017-09-11 字数2080 浏览 871 评论 0

sanitize_hex_color( string $color )

Sanitizes a hex color.


描述

Returns either ”, a 3 or 6 digit hex color (with


参数

$color

(string)
(Required)


返回值

(string|void)


源代码

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;
	}
}

更新日志

Version 描述
3.4.0 Introduced.

相关函数

Used By

  • wp-includes/formatting.php:
    sanitize_hex_color_no_hash()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note

    Contributed by simonrcodrington

    Note: This function cannot be used outside of the theme customizer. It is only loaded when using the customizer interface. If you try and use this anywhere else (I.e inside a custom menu or edit screen) it will trigger a fatal error.

  2. 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',
    ) );
    

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!