函数文档

wp_filter_pre_oembed_result()

💡 云策文档标注

概述

wp_filter_pre_oembed_result() 是一个 WordPress 过滤器,用于在发起 HTTP 请求之前修改 oEmbed 结果。如果 URL 属于当前站点,则直接获取结果,跳过 oEmbed 发现过程。

关键要点

  • 过滤器在 oEmbed HTTP 请求前执行,允许自定义嵌入 HTML。
  • 参数包括 $result(未净化的 HTML)、$url(URL)和 $args(oEmbed 参数)。
  • 返回未净化的 HTML 或 null(如果 URL 不属于当前站点)。
  • 内部使用 get_oembed_response_data_for_url() 和 _wp_oembed_get_object() 函数。
  • 自 WordPress 4.5.3 版本引入。

代码示例

function wp_filter_pre_oembed_result( $result, $url, $args ) {
    $data = get_oembed_response_data_for_url( $url, $args );

    if ( $data ) {
        return _wp_oembed_get_object()->data2html( $data, $url );
    }

    return $result;
}

📄 原文内容

Filters the oEmbed result before any HTTP requests are made.

Description

If the URL belongs to the current site, the result is fetched directly instead of going through the oEmbed discovery process.

Parameters

$resultnull|stringrequired
The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
$urlstringrequired
The URL that should be inspected for discovery <link> tags.
$argsarrayrequired
oEmbed remote get arguments.

Return

null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
Null if the URL does not belong to the current site.

Source

function wp_filter_pre_oembed_result( $result, $url, $args ) {
	$data = get_oembed_response_data_for_url( $url, $args );

	if ( $data ) {
		return _wp_oembed_get_object()->data2html( $data, $url );
	}

	return $result;
}

Changelog

Version Description
4.5.3 Introduced.