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 `<script>` tag attributes.
    Required: No
    Default: array()
Returns:
  • (string) String containing inline JavaScript code wrapped around `<script>` tag.
Defined at:
Codex:

Constructs an inline script tag.

It is possible to inject attributes in the <script> tag via the {@see 'wp_inline_script_attributes'} filter. Automatically injects type attribute if needed.


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 );

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