wppaste
WordPress

wp_get_inline_script_tag( string $data, $attributes = array() ): string

Since
5.7.0, 7.0.0
Source
wp-includes/script-loader.php:3021
Constructs an inline script tag.

Description

It is possible to inject attributes in the <script> tag via the 'wp_inline_script_attributes' filter.

If the $data is unsafe to embed in a <script> tag, an empty script tag with the provided attributes will be returned. JavaScript and JSON contents can be escaped, so this is only likely to be a problem with unusual content types.

Example:

// The dangerous JavaScript in this example will be safely escaped.
// A string with the script tag and the desired contents will be returned.
wp_get_inline_script_tag( 'console.log( "</script>" );' );

// This data is unsafe and text/plain cannot be escaped.
// The following will return "" to indicate failure:
wp_get_inline_script_tag( '</script>', array( 'type' => 'text/plain' ) );

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$datastring
Data for script tag: JavaScript, importmap, speculationrules, etc.
$attributesoptional
Default: array()

Return value

string
HTML script tag containing the provided $data or the empty string "" if the data cannot be safely embedded in a script tag.

Performance profile

How much work a call to wp_get_inline_script_tag() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
25–29

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 43.

Plugin surface
1 hook

Third-party callbacks on 'wp_inline_script_attributes' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_inline_script_tag() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!->set_modifiable_text()25–26apply_filters(), ->next_tag(), ->set_modifiable_text()
->set_modifiable_text()28–29apply_filters(), ->next_tag(), ->set_modifiable_text(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4325–295
8.54325–295
8.44325–2953 fewer instructions than PHP 8.3
8.34628–325
8.24628–325
8.14628–325
7.44628–325

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_get_inline_script_tag() runs, in this order:

  1. apply_filters( wp_inline_script_attributes )filterline 3034 (+13 into the body)

    Filters attributes to be added to a script tag.

Uses · 2

Used by · 8

Source code

function wp_get_inline_script_tag( $data, $attributes = array() ) {	$data = "\n" . trim( $data, "\n\r " ) . "\n"; 	/**	 * Filters attributes to be added to a script tag.	 *	 * @since 5.7.0	 *	 * @param array<string, string|bool> $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                     $data       Inline data.	 */	$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data ); 	$processor = new WP_HTML_Tag_Processor( '<script></script>' );	$processor->next_tag();	foreach ( $attributes as $name => $value ) {		/*		 * Lexical variations of an attribute name may represent the		 * same attribute in HTML, therefore it’s possible that the		 * input array might contain duplicate attributes even though		 * it’s keyed on their name. Calling code should rewrite an		 * attribute’s value rather than sending a duplicate attribute.		 *		 * Example:		 *		 *     array( 'id' => 'main', 'ID' => 'nav' )		 *		 * In this example, there are two keys both describing the `id`		 * attribute. PHP array iteration is in key-insertion order so		 * the 'id' value will be set in the SCRIPT tag.		 */		if ( null !== $processor->get_attribute( $name ) ) {			continue;		} 		$processor->set_attribute( $name, $value ?? true );	} 	if ( ! $processor->set_modifiable_text( $data ) ) {		return '';	} 	return "{$processor->get_updated_html()}\n";}

Changelog

Introduced in 5.7.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Parameter $attributes retyped from array to untyped.verified against source
7.0.0
Returns an empty string if the data cannot be safely embedded in a script tag.from the docblock
5.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/script-loader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.