wp_get_inline_script_tag() WordPress Function

The wp_get_inline_script_tag() function allows you to easily add inline JavaScript to your WordPress site. This function can be useful for adding small bits of JavaScript code to your site without having to create a separate file for it.

wp_get_inline_script_tag( string $javascript, array $attributes = array() ) #

Wraps inline JavaScript in tag.


Description

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


Top ↑

Parameters

$javascript

(string)(Required)Inline JavaScript code.

$attributes

(array)(Optional) Key-value pairs representing <script> tag attributes.

Default value: array()


Top ↑

Return

(string) String containing inline JavaScript code wrapped around <script> tag.


Top ↑

Source

File: wp-includes/script-loader.php

function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
	if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
		$attributes['type'] = 'text/javascript';
	}
	/**
	 * Filters attributes to be added to a script tag.
	 *
	 * @since 5.7.0
	 *
	 * @param array  $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 $javascript Inline JavaScript code.
	 */
	$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $javascript );

	$javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";

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


Top ↑

Changelog

Changelog
VersionDescription
5.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More