On this page
wp_sanitize_script_attributes( array $attributes ): string
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
$attributesarray Required-
Key-value pairs representing
<script>tag attributes.
Return
string String made of sanitized <script> tag attributes.
Source
File: wp-includes/script-loader.php. View all references
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;
}
Related
Uses
| Uses | Description |
|---|---|
| current_theme_supports() wp-includes/theme.php | Checks a theme’s support for a given feature. |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| is_admin() wp-includes/load.php | Determines whether the current request is for an administrative interface page. |
Used By
| Used By | Description |
|---|---|
| wp_get_inline_script_tag() wp-includes/script-loader.php | Wraps inline JavaScript in tag. |
| wp_get_script_tag() wp-includes/script-loader.php | Formats loader tags. |
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_sanitize_script_attributes