wp_sanitize_script_attributes

函式
wp_sanitize_script_attributes ( $attributes )
引數
  • (array) $attributes Key-value pairs representing `<script>` tag attributes.
    Required:
返回值
  • (string) String made of sanitized `<script>` tag attributes.
定義位置
相關方法
wp_kses_uri_attributesserialize_block_attributeswp_pre_kses_block_attributeswp_initialize_sitewp_localize_script
引入
5.7.0
棄用
-

wp_sanitize_script_attributes: 這是一個WordPress的函式,用於對指令碼標籤的屬性進行淨化。它刪除了任何潛在的惡意字元,並確保指令碼只從受信任的來源載入。

將一個屬性陣列淨化成一個屬性字串,放在`<script>`標籤內。

如果需要,自動注入型別屬性。由{@see}和{@see}使用。

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 ? sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), 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;
}

常見問題

FAQs
檢視更多 >