wp_oembed_get()
云策文档标注
概述
wp_oembed_get() 函数用于通过 oEmbed 协议获取指定 URL 的嵌入 HTML 代码。它调用 WP_oEmbed 对象的方法来处理请求,并返回成功时的嵌入 HTML 或失败时的 false。
关键要点
- 函数接受两个参数:必需的 $url(要嵌入的 URL)和可选的 $args(附加参数数组或字符串,默认为空)。
- $args 可包含 width、height 和 discover 等选项,用于控制嵌入尺寸和提供者发现行为。
- 返回值为字符串(成功时的嵌入 HTML)或 false(失败时)。
- 内部依赖 _wp_oembed_get_object() 函数获取 WP_oEmbed 对象实例。
- 自 WordPress 2.9.0 版本引入,相关用途包括媒体小部件渲染和短码处理。
代码示例
// 从支持的 oEmbed 提供者获取嵌入代码,带宽度参数
$embed_code = wp_oembed_get( 'http://www.youtube.com/watch?v=AbcDeFg123', array( 'width' => 400 ) );// 从支持的 oEmbed 提供者获取嵌入代码,无附加参数
$embed_code = wp_oembed_get( 'http://www.youtube.com/watch?v=AbcDeFg123' );
原文内容
Attempts to fetch the embed HTML for a provided URL using oEmbed.
Description
See also
Parameters
$urlstringrequired-
The URL that should be embedded.
$argsarray|stringoptional-
Additional arguments for retrieving embed HTML. Default empty.
widthint|stringOptional. Themaxwidthvalue passed to the provider URL.heightint|stringOptional. Themaxheightvalue passed to the provider URL.discoverboolOptional. 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
function wp_oembed_get( $url, $args = '' ) {
$oembed = _wp_oembed_get_object();
return $oembed->get_html( $url, $args );
}
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
Skip to note 3 content
Codex
Retrieve the embed code for a URL from a supported
oEmbedprovider – withwidthargument:$embed_code = wp_oembed_get( 'http://www.youtube.com/watch?v=AbcDeFg123', array( 'width' => 400 ) );Skip to note 4 content
Codex
Retrieve the embed code for a URL from a supported
oEmbedprovider:$embed_code = wp_oembed_get( 'http://www.youtube.com/watch?v=AbcDeFg123' );