wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_inline_script_tag › WordPress Function

Since5.7.0
Deprecatedn/a
wp_get_inline_script_tag ( $data, $attributes = array() )
Parameters: (2)
  • (string) $data Data for script tag: JavaScript, importmap, speculationrules, etc.
    Required: Yes
  • (array<string,string|bool>) $attributes Optional. Key-value pairs representing `&lt;script&gt;` tag attributes.
    Required: No
    Default: array()
Returns:
  • (string) HTML script tag containing the provided $data or the empty string `""` if the data cannot be safely embedded in a script tag.
Defined at:
Codex:
Change Log:
  • 7.0.0

Constructs an inline script tag.

It is possible to inject attributes in the <script> tag via the {@see '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( "" );' ); // This data is unsafe and text/plain cannot be escaped. // The following will return "" to indicate failure: wp_get_inline_script_tag( '', array( 'type' => 'text/plain' ) );


Source

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 ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			__( 'Unable to set inline script data.' ),
			'7.0.0'
		);
		return '';
	}

	return "{$processor->get_updated_html()}\n";
}