wp_get_inline_script_tag

函式
wp_get_inline_script_tag ( $javascript, $attributes = array() )
引數
  • (string) $javascript Inline JavaScript code.
    Required:
  • (array) $attributes Optional. Key-value pairs representing `<script>` tag attributes.
    Required:
    Default: array()
返回值
  • (string) String containing inline JavaScript code wrapped around `<script>` tag.
定義位置
相關方法
wp_print_inline_script_tagwp_get_script_tagwp_add_inline_scriptwp_print_script_tagwp_tinymce_inline_scripts
引入
5.7.0
棄用
-

wp_get_inline_script_tag: 這個函式用來為一個內聯指令碼生成一個指令碼標籤。它接受一個包含指令碼內容的字串作為唯一的引數,並返回一個HTML指令碼標籤,該標籤可用於在網頁中包含該指令碼。

在`<script>`標籤中包裹內聯的JavaScript。

可以通過{@see ‘wp_script_attributes’}過濾器在“標籤中注入屬性。
如果需要,會自動注入型別屬性。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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, "nr " ) . "n";
return sprintf( "<script%s>%s</script>n", wp_sanitize_script_attributes( $attributes ), $javascript );
}
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, "nr " ) . "n"; return sprintf( "<script%s>%s</script>n", wp_sanitize_script_attributes( $attributes ), $javascript ); }
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, "nr " ) . "n";

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

常見問題

FAQs
檢視更多 >