钩子文档

oembed_result

💡 云策文档标注

概述

oembed_result 过滤器用于修改从 oEmbed 提供程序返回的 HTML 内容。它允许开发者在 oEmbed 响应被缓存到 postmeta 表之前,对嵌入的 HTML 进行自定义处理。

关键要点

  • 过滤器名称:oembed_result
  • 参数:$data(返回的 oEmbed HTML 或 false 表示不安全)、$url(要嵌入内容的 URL)、$args(检索嵌入 HTML 的额外参数)
  • 应用时机:在 oEmbed 响应被缓存为 _oembed_* 元数据之前调用
  • 性能注意:与 embed_oembed_html 过滤器相比,oembed_result 仅在首次获取 oEmbed 时触发,可能更高效

注意事项

此过滤器在 oEmbed 响应被缓存前应用,因此如果需要对所有嵌入 URL 的 HTML 进行修改,应考虑使用 embed_oembed_html 过滤器,但需注意其可能影响性能。


📄 原文内容

Filters the HTML returned by the oEmbed provider.

Parameters

$datastring|false
The returned oEmbed HTML (false if unsafe).
$urlstring
URL of the content to be embedded.
$argsstring|array
Additional arguments for retrieving embed HTML.
See wp_oembed_get() for accepted arguments. Default empty.

More Arguments from wp_oembed_get( … $args )

Additional arguments for retrieving embed HTML. Default empty.

  • width int|string
    Optional. The maxwidth value passed to the provider URL.
  • height int|string
    Optional. The maxheight value passed to the provider URL.
  • discover bool
    Optional. Determines whether to attempt to discover link tags at the given URL for an oEmbed provider when the provider URL is not found in the built-in providers list. Default true.

Source

return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    In case you are wondering why this filter does not work for you:
    The oembed_result filter is applied before the oEmbed response is cached as _oembed_* meta entry in the postmeta table.
    If you want to always modify the oembed HTML, you should look at embed_oembed_html filter.
    However, using the `embed_oembed_html` filter may reduce site’s performance, because it is called on every page load for every embed URL.