wp_get_inline_script_tag( string $data, $attributes = array() ): string
- Since
- 5.7.0, 7.0.0
- Source
wp-includes/script-loader.php:3021
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
- Scaling
- Scales with input
- Instructions
- 25–29
- Plugin surface
- 1 hook
- Called by
- 8
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 43.
Third-party callbacks on 'wp_inline_script_attributes' run inside this call, and their cost is not bounded by anything here.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!->set_modifiable_text() | 25–26 | apply_filters(), ->next_tag(), ->set_modifiable_text() |
->set_modifiable_text() | 28–29 | apply_filters(), ->next_tag(), ->set_modifiable_text(), ->get_updated_html() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 43 | 25–29 | 5 | |
| 8.5 | 43 | 25–29 | 5 | |
| 8.4 | 43 | 25–29 | 5 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 46 | 28–32 | 5 | |
| 8.2 | 46 | 28–32 | 5 | |
| 8.1 | 46 | 28–32 | 5 | |
| 7.4 | 46 | 28–32 | 5 |
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:
- apply_filters( wp_inline_script_attributes )filterline 3034 (+13 into the body)
Filters attributes to be added to a script tag.
Uses · 2
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_HTML_Tag_Processor::__construct()Constructor.
Used by · 8
- WP_Customize_Manager::wp_die()Custom wp_die wrapper. Returns either the standard message for UI or the Ajax message.
- WP_Scripts::do_item()Processes a script dependency.
- WP_Scripts::get_inline_script_tag()Gets tags for inline scripts registered for a specific handle.
- block_core_archives_build_dropdown_script()Generates the inline script for an archives dropdown field.
- build_dropdown_script_block_core_categories()Generates the inline script for a categories dropdown field.
- get_post_embed_html()Retrieves the embed code for a specific post.
- wp_admin_bar_command_palette_menu()Adds the command palette trigger button.
- wp_print_inline_script_tag()Prints an inline script tag.
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.
Signature, return type and hooks compared across 5 parsed releases.
$attributes retyped from array to untyped.verified against sourceAbout 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.