函数文档

wp_oembed_add_host_js()

💡 云策文档标注

概述

wp_oembed_add_host_js() 函数用于添加与嵌入 iframe 通信所需的 JavaScript,但自 WordPress 5.9.0 起已弃用,建议使用 wp_maybe_enqueue_oembed_host_js() 替代。该函数目前仅作为向后兼容的钩子存在,以确保旧代码仍能正常工作。

关键要点

  • wp_oembed_add_host_js() 已弃用,推荐使用 wp_maybe_enqueue_oembed_host_js()。
  • 函数在 default-filters.php 中通过 add_action('wp_head', 'wp_oembed_add_host_js') 注册,用于添加 oEmbed 主机脚本。
  • 可通过 remove_action('wp_head', 'wp_oembed_add_host_js') 禁用脚本添加,wp_maybe_enqueue_oembed_host_js() 会检查此钩子是否被移除。
  • 函数本身为空实现,仅作为钩子标识符,确保向后兼容性。

注意事项

自 WordPress 5.9.0 起,此函数已被弃用,开发者应更新代码以避免使用。


📄 原文内容

Adds the necessary JavaScript to communicate with the embedded iframes.

Description

This function is no longer used directly. For back-compat it exists exclusively as a way to indicate that the oEmbed host JS should be added. In default-filters.php there remains this code:

add_action( 'wp_head', 'wp_oembed_add_host_js' )

Historically a site has been able to disable adding the oEmbed host script by doing:

remove_action( 'wp_head', 'wp_oembed_add_host_js' )

In order to ensure that such code still works as expected, this function remains. There is now a has_action() check in wp_maybe_enqueue_oembed_host_js() to see if wp_oembed_add_host_js() has not been unhooked from running at the wp_head action.

Source

function wp_oembed_add_host_js() {}

Changelog

Version Description
5.9.0 Deprecated. Use wp_maybe_enqueue_oembed_host_js() instead.
4.4.0 Introduced.