函数文档

print_emoji_detection_script()

💡 云策文档标注

概述

print_emoji_detection_script() 是一个 WordPress 函数,用于确保内联 Emoji 检测脚本仅被打印一次。它通过静态变量跟踪打印状态,并根据 wp_print_footer_scripts 钩子的触发情况来执行或延迟打印。

关键要点

  • 函数使用静态变量 $printed 来防止脚本重复打印,确保只执行一次。
  • 如果 wp_print_footer_scripts 钩子已触发,则直接调用 _print_emoji_detection_script();否则,通过 add_action() 将该函数添加到钩子中。
  • 此函数自 WordPress 4.2.0 版本引入,依赖于 did_action() 和 add_action() 等核心函数。

代码示例

function print_emoji_detection_script() {
    static $printed = false;

    if ( $printed ) {
        return;
    }

    $printed = true;

    if ( did_action( 'wp_print_footer_scripts' ) ) {
        _print_emoji_detection_script();
    } else {
        add_action( 'wp_print_footer_scripts', '_print_emoji_detection_script' );
    }
}

注意事项

  • 确保在需要 Emoji 检测的页面中调用此函数,以避免脚本缺失或重复加载。
  • 理解 did_action() 和 add_action() 的使用,以便在自定义开发中正确处理钩子时机。

📄 原文内容

Prints the inline Emoji detection script if it is not already printed.

Source

function print_emoji_detection_script() {
	static $printed = false;

	if ( $printed ) {
		return;
	}

	$printed = true;

	if ( did_action( 'wp_print_footer_scripts' ) ) {
		_print_emoji_detection_script();
	} else {
		add_action( 'wp_print_footer_scripts', '_print_emoji_detection_script' );
	}
}

Changelog

Version Description
4.2.0 Introduced.