Title: wp_get_inline_script_tag
Published: March 9, 2021
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#wp--skip-link--target)

Constructs an inline script tag.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#description)󠁿

It is possible to inject attributes in the `<script>` tag via the [‘wp_inline_script_attributes’](https://developer.wordpress.org/reference/hooks/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:

    ```php
    // 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' ) );
    ```

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#parameters)󠁿

 `$data`stringrequired

Data for script tag: JavaScript, importmap, speculationrules, etc.

`string|bool`> $attributes Optional. Key-value pairs representing `<script>` tag
attributes.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#source)󠁿

    ```php
    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";
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/script-loader.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/script-loader.php#L3021)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/script-loader.php#L3021-L3066)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_inline_script_attributes’, array<string, , string $data )](https://developer.wordpress.org/reference/hooks/wp_inline_script_attributes/)

Filters attributes to be added to a script tag.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_HTML_Tag_Processor::__construct()](https://developer.wordpress.org/reference/classes/wp_html_tag_processor/__construct/)`wp-includes/html-api/class-wp-html-tag-processor.php` |

Constructor.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [wp_admin_bar_command_palette_menu()](https://developer.wordpress.org/reference/functions/wp_admin_bar_command_palette_menu/)`wp-includes/admin-bar.php` |

Adds the command palette trigger button.

  | 
| [WP_Scripts::get_inline_script_tag()](https://developer.wordpress.org/reference/classes/wp_scripts/get_inline_script_tag/)`wp-includes/class-wp-scripts.php` |

Gets tags for inline scripts registered for a specific handle.

  | 
| [wp_print_inline_script_tag()](https://developer.wordpress.org/reference/functions/wp_print_inline_script_tag/)`wp-includes/script-loader.php` |

Prints an inline script tag.

  | 
| [get_post_embed_html()](https://developer.wordpress.org/reference/functions/get_post_embed_html/)`wp-includes/embed.php` |

Retrieves the embed code for a specific post.

  | 
| [WP_Customize_Manager::wp_die()](https://developer.wordpress.org/reference/classes/wp_customize_manager/wp_die/)`wp-includes/class-wp-customize-manager.php` |

Custom wp_die wrapper. Returns either the standard message for UI or the Ajax message.

  | 
| [WP_Scripts::do_item()](https://developer.wordpress.org/reference/classes/wp_scripts/do_item/)`wp-includes/class-wp-scripts.php` |

Processes a script dependency.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#changelog)󠁿

| Version | Description | 
| [7.0.0](https://developer.wordpress.org/reference/since/7.0.0/) | Returns an empty string if the data cannot be safely embedded in a script tag. | 
| [5.7.0](https://developer.wordpress.org/reference/since/5.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#comment-content-4943)
 2.    [albertomake](https://profiles.wordpress.org/albertomake/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/#comment-4943)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%23comment-4943)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%23comment-4943)
 4.  A nice way to include Google Analytics and other scripts in the <head> of the 
     document:
 5.      ```php
         function wpdocs_add_scripts_head() {
         		// Google Analytics
         		$ga_script = "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create', 'UA-XXXXX-Y', 'auto');ga('send', 'pageview');";
         		echo wp_get_inline_script_tag( $ga_script );
         		echo wp_get_script_tag( array(
         			'async' => true,
         			'src' => 'https://www.google-analytics.com/analytics.js'
         		) );
         	}
         	add_action( 'wp_head', 'wpdocs_add_scripts_head', 1 );
         ```
     
 6.  Will output:
 7.  <script>
      window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.
     l=+new Date;ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’);ga(‘send’, ‘pageview’); </script
     > <script async src="[https://www.google-analytics.com/analytics.js"></script>%5B/php%5D](https://www.google-analytics.com/analytics.js"></script>%5B/php%5D)
 8.  This article has good examples [https://make.wordpress.org/core/2021/02/23/introducing-script-attributes-related-functions-in-wordpress-5-7/](https://make.wordpress.org/core/2021/02/23/introducing-script-attributes-related-functions-in-wordpress-5-7/)
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%3Freplytocom%3D4943%23feedback-editor-4943)
 10.  [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/?output_format=md#comment-content-6744)
 11.   Anonymous User  [  3 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/#comment-6744)
 12. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%23comment-6744)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%23comment-6744)
 13. This has to be used to make WP a CSP compliant system (at least, in the front 
     end. Remains to be tested in the admin area)
 14.     ```php
         function wpdocs_add_nonce_to_scripts( $attr ) {
         	if ( 'text/javascript' !== $attr['type'] ) {
         		return $attr;
         	}
     
         	return array(
         		'type' => 'text/javascript',
         		'nonce' => '123',// Your Nonce. Obviously more featured than this example.
         	);
         }
         add_filter( 'wp_inline_script_attributes', 'wpdocs_add_nonce_to_scripts' );
         ```
     
 15. Then, you can use `'nonce-123'` in your CSP Policy, example:
      `"script-src 'self''
     noncoe-123';"`
 16.  * Note that this will override other attributes on the `script` tag. Would be
        better to set `$attr['nonce']` on the existing array rather than return a new
        array.
      * [crstauf](https://profiles.wordpress.org/crstauf/) [3 years ago](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/#comment-6749)
 17.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F%3Freplytocom%3D6744%23feedback-editor-6744)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_inline_script_tag%2F)
before being able to contribute a note or feedback.