函数文档

wp_just_in_time_script_localization()

💡 云策文档标注

概述

wp_just_in_time_script_localization() 函数用于在脚本打印时加载本地化数据,而非初始化阶段,适用于需要动态信息的场景。

关键要点

  • 函数在打印时加载本地化数据,避免因信息未就绪导致的初始化问题。
  • 内部调用 wp_localize_script() 为多个脚本(如 autosave、mce-view、word-count)提供本地化数据。
  • 本地化数据包括 AUTOSAVE_INTERVAL、blog_id、shortcodes 等动态内容。

代码示例

function wp_just_in_time_script_localization() {

	wp_localize_script(
		'autosave',
		'autosaveL10n',
		array(
			'autosaveInterval' => AUTOSAVE_INTERVAL,
			'blog_id'          => get_current_blog_id(),
		)
	);

	wp_localize_script(
		'mce-view',
		'mceViewL10n',
		array(
			'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
		)
	);

	wp_localize_script(
		'word-count',
		'wordCountL10n',
		array(
			'type'       => wp_get_word_count_type(),
			'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
		)
	);
}

注意事项

  • 此函数自 WordPress 2.5.0 版本引入,适用于需要延迟加载本地化数据的场景。
  • 相关函数包括 wp_get_word_count_type()、wp_localize_script() 和 get_current_blog_id()。

📄 原文内容

Loads localized data on print rather than initialization.

Description

These localizations require information that may not be loaded even by init.

Source

function wp_just_in_time_script_localization() {

	wp_localize_script(
		'autosave',
		'autosaveL10n',
		array(
			'autosaveInterval' => AUTOSAVE_INTERVAL,
			'blog_id'          => get_current_blog_id(),
		)
	);

	wp_localize_script(
		'mce-view',
		'mceViewL10n',
		array(
			'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
		)
	);

	wp_localize_script(
		'word-count',
		'wordCountL10n',
		array(
			'type'       => wp_get_word_count_type(),
			'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
		)
	);
}

Changelog

Version Description
2.5.0 Introduced.