WP_Scripts::print_extra_script() WordPress Method

The WP_Scripts::print_extra_script() method is used to print extra scripts before the closing body tag on the front end of a WordPress site. This is useful for adding custom JavaScript code or for including JavaScript files that are needed on certain pages.

WP_Scripts::print_extra_script( string $handle, bool $display = true ) #

Prints extra scripts of a registered script.


Parameters

$handle

(string)(Required)The script's registered handle.

$display

(bool)(Optional) Whether to print the extra script instead of just returning it.

Default value: true


Top ↑

Return

(bool|string|void) Void if no data exists, extra scripts if $display is true, true otherwise.


Top ↑

Source

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

	public function print_extra_script( $handle, $display = true ) {
		$output = $this->get_data( $handle, 'data' );
		if ( ! $output ) {
			return;
		}

		if ( ! $display ) {
			return $output;
		}

		printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );

		// CDATA is not needed for HTML 5.
		if ( $this->type_attr ) {
			echo "/* <![CDATA[ */\n";
		}

		echo "$output\n";

		if ( $this->type_attr ) {
			echo "/* ]]> */\n";
		}

		echo "</script>\n";

		return true;
	}


Top ↑

Changelog

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