wp_get_inline_script_tag() – Wraps inline JavaScript in `<script>` tag.

You appear to be a bot. Output may be restricted

Description

Wraps inline JavaScript in <script> tag.

It is possible to inject attributes in the <script> tag via the wp_script_attributes filter. Automatically injects type attribute if needed.

Usage

$string = wp_get_inline_script_tag( $javascript, $attributes );

Parameters

$javascript
( string ) required – Inline JavaScript code.
$attributes
( array ) optional – Optional. Key-value pairs representing <script> tag attributes.

Returns

string String containing inline JavaScript code wrapped around <script> tag.

Source

File name: wordpress/wp-includes/script-loader.php
Lines:

1 to 20 of 20
function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
  if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
    $attributes['type'] = 'text/javascript';
  }
  
/**
 * Filters attributes to be added to a script tag.
 *
 * @since 5.7.0
 *
 * @param array  $attributes Key-value pairs representing `<script>` tag attributes.
 *                           Only the attribute name is added to the `<script>` tag for
 *                           entries with a boolean value, and that are true.
 * @param string $javascript Inline JavaScript code.
 */
  $attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $javascript );

  $javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";

  return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $javascript );
}
 

 View on GitHub View on Trac