WP_Scripts::add_inline_script() WordPress Method

The WP_Scripts::add_inline_script() method allows you to add inline JavaScript to a registered script. This is useful if you need to add JavaScript to a script that is already registered, or if you need to add JavaScript to a script that is not registered.

WP_Scripts::add_inline_script( string $handle, string $data, string $position = 'after' ) #

Adds extra code to a registered script.


Parameters

$handle

(string)(Required)Name of the script to add the inline script to. Must be lowercase.

$data

(string)(Required)String containing the JavaScript to be added.

$position

(string)(Optional) Whether to add the inline script before the handle or after.

Default value: 'after'


Top ↑

Return

(bool) True on success, false on failure.


Top ↑

Source

File: wp-includes/class.wp-scripts.php

	public function add_inline_script( $handle, $data, $position = 'after' ) {
		if ( ! $data ) {
			return false;
		}

		if ( 'after' !== $position ) {
			$position = 'before';
		}

		$script   = (array) $this->get_data( $handle, $position );
		$script[] = $data;

		return $this->add_data( $handle, $position, $script );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.5.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.