wp_sanitize_script_attributes()
云策文档标注
概述
wp_sanitize_script_attributes() 函数用于将属性数组转换为可安全插入 script 标签内的属性字符串。它自动注入必要的 type 属性,并被 wp_get_script_tag() 和 wp_get_inline_script_tag() 函数调用。
关键要点
- 将属性数组转换为 script 标签内的属性字符串
- 自动注入 type 属性(如需要)
- 被 wp_get_script_tag() 和 wp_get_inline_script_tag() 使用
- 参数为必需的 $attributes 数组,表示键值对
原文内容
Sanitizes an attributes array into an attributes string to be placed inside a tag.
Description
Automatically injects type attribute if needed.
Used by wp_get_script_tag() and wp_get_inline_script_tag().
Parameters
$attributesarrayrequired- Key-value pairs representing
<script>tag attributes.
Source
function wp_sanitize_script_attributes( $attributes ) {
$html5_script_support = is_admin() || current_theme_supports( 'html5', 'script' );
$attributes_string = '';
/*
* If HTML5 script tag is supported, only the attribute name is added
* to $attributes_string for entries with a boolean value, and that are true.
*/
foreach ( $attributes as $attribute_name => $attribute_value ) {
if ( is_bool( $attribute_value ) ) {
if ( $attribute_value ) {
$attributes_string .= $html5_script_support ? ' ' . esc_attr( $attribute_name ) : sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_name ) );
}
} else {
$attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
}
}
return $attributes_string;
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.