wp_print_inline_script_tag( string $data, array $attributes = array() )

Prints an inline script tag.

Description

It is possible to inject attributes in the <script> tag via the ‘wp_inline_script_attributes’ filter.
Automatically injects type attribute if needed.

Parameters

$datastringrequired
Data for script tag: JavaScript, importmap, speculationrules, etc.
$attributesarrayoptional
Key-value pairs representing <script> tag attributes.

Default:array()

Source

function wp_print_inline_script_tag( $data, $attributes = array() ) {
	echo wp_get_inline_script_tag( $data, $attributes );
}

Changelog

VersionDescription
5.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This example uses wp_print_inline_script_tag() to securely pass the REST API URL and a security nonce to a global JavaScript object for front-end requests.

    function wpdocs_print_rest_api_settings() {
    	$settings = array(
    		'root'  => esc_url_raw( get_rest_url() ),
    		'nonce' => wp_create_nonce( 'wp_rest' ),
    	);
    
    	wp_print_inline_script_tag(
    		'window.wpdocsApiSettings = ' . wp_json_encode( $settings ) . ';',
    		array( 'id' => 'wpdocs-api-settings' )
    	);
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_print_rest_api_settings' );

You must log in before being able to contribute a note or feedback.