wp_add_inline_script

函式
wp_add_inline_script ( $handle, $data, $position = 'after' )
引數
  • (string) $handle Name of the script to add the inline script to.
    Required:
  • (string) $data String containing the JavaScript to be added.
    Required:
  • (string) $position Optional. Whether to add the inline script before the handle or after. Default 'after'.
    Required:
    Default: 'after'
返回值
  • (bool) True on success, false on failure.
相關
  • WP_Scripts::add_inline_script()
定義位置
相關方法
wp_add_inline_stylewp_get_inline_script_tagwp_tinymce_inline_scriptswp_localize_scriptwp_print_inline_script_tag
引入
4.5.0
棄用
-

wp_add_inline_script: 這個函式將內聯指令碼新增到頁面中。它允許開發人員直接將JavaScript程式碼新增到頁面中,而不需要建立一個單獨的指令碼檔案: 這個函式需要三個引數:一個指令碼控制代碼(指令碼的唯一識別符號),JavaScript程式碼,以及一個可選的依賴性陣列: 該函式將在一個具有指定控制代碼的指令碼標籤中輸出指令碼程式碼。

向已註冊的指令碼新增額外的程式碼。

只有當指令碼已經在佇列中時,程式碼才會被新增。接受一個包含程式碼的字串$data。如果兩個或更多的程式碼塊被新增到同一個指令碼$handle中,它們將按照新增的順序被列印出來,也就是說,後面新增的程式碼可以重新宣告前面的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
if ( false !== stripos( $data, '</script>' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: <script>, 2: wp_add_inline_script() */
__( 'Do not pass %1$s tags to %2$s.' ),
'<code>&lt;script&gt;</code>',
'<code>wp_add_inline_script()</code>'
),
'4.5.0'
);
$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
}
return wp_scripts()->add_inline_script( $handle, $data, $position );
}
function wp_add_inline_script( $handle, $data, $position = 'after' ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); if ( false !== stripos( $data, '</script>' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: <script>, 2: wp_add_inline_script() */ __( 'Do not pass %1$s tags to %2$s.' ), '<code>&lt;script&gt;</code>', '<code>wp_add_inline_script()</code>' ), '4.5.0' ); $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) ); } return wp_scripts()->add_inline_script( $handle, $data, $position ); }
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	if ( false !== stripos( $data, '</script>' ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: <script>, 2: wp_add_inline_script() */
				__( 'Do not pass %1$s tags to %2$s.' ),
				'<code>&lt;script&gt;</code>',
				'<code>wp_add_inline_script()</code>'
			),
			'4.5.0'
		);
		$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
	}

	return wp_scripts()->add_inline_script( $handle, $data, $position );
}

常見問題

FAQs
檢視更多 >