get_oembed_response_data()
云策文档标注
概述
get_oembed_response_data() 函数用于获取指定文章的 oEmbed 响应数据,包括标题、作者、提供者信息等。它验证文章存在性、公开可查看性和可嵌入性,并处理宽度参数以生成标准化的响应数组。
关键要点
- 参数:接受 $post(WP_Post 对象或整数 ID)和 $width(整数宽度),均为必需。
- 返回值:成功时返回包含 oEmbed 数据的数组,失败时返回 false(如文章不存在、非公开或不可嵌入)。
- 验证步骤:检查文章是否存在、是否公开可查看(is_post_publicly_viewable)和是否可嵌入(is_post_embeddable)。
- 宽度处理:通过 oembed_min_max_width 过滤器限制宽度在默认 200 到 600 像素之间,并计算相应高度。
- 数据构建:返回数组包含版本、提供者名称、URL、作者信息、标题和类型等字段。
- 过滤器:提供 oembed_min_max_width 和 oembed_response_data 两个过滤器,允许开发者自定义宽度限制和响应数据。
代码示例
function get_oembed_response_data( $post, $width ) {
$post = get_post( $post );
$width = absint( $width );
if ( ! $post ) {
return false;
}
if ( ! is_post_publicly_viewable( $post ) ) {
return false;
}
if ( ! is_post_embeddable( $post ) ) {
return false;
}
$min_max_width = apply_filters(
'oembed_min_max_width',
array(
'min' => 200,
'max' => 600,
)
);
$width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
$height = max( (int) ceil( $width / 16 * 9 ), 200 );
$data = array(
'version' => '1.0',
'provider_name' => get_bloginfo( 'name' ),
'provider_url' => get_home_url(),
'author_name' => get_bloginfo( 'name' ),
'author_url' => get_home_url(),
'title' => get_the_title( $post ),
'type' => 'link',
);
$author = get_userdata( $post->post_author );
if ( $author ) {
$data['author_name'] = $author->display_name;
$data['author_url'] = get_author_posts_url( $author->ID );
}
return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
}注意事项
- 文章必须支持嵌入(通过 is_post_embeddable 检查),否则返回 false。
- 宽度参数会被限制在通过 oembed_min_max_width 过滤器设置的范围内,默认最小 200 像素,最大 600 像素。
- 高度基于宽度按 16:9 比例计算,但至少为 200 像素。
- 响应数据可通过 oembed_response_data 过滤器进行自定义修改。
原文内容
Retrieves the oEmbed response data for a given post.
Parameters
$postWP_Post|intrequired-
Post ID or post object.
$widthintrequired-
The requested width.
Source
function get_oembed_response_data( $post, $width ) {
$post = get_post( $post );
$width = absint( $width );
if ( ! $post ) {
return false;
}
if ( ! is_post_publicly_viewable( $post ) ) {
return false;
}
if ( ! is_post_embeddable( $post ) ) {
return false;
}
/**
* Filters the allowed minimum and maximum widths for the oEmbed response.
*
* @since 4.4.0
*
* @param array $min_max_width {
* Minimum and maximum widths for the oEmbed response.
*
* @type int $min Minimum width. Default 200.
* @type int $max Maximum width. Default 600.
* }
*/
$min_max_width = apply_filters(
'oembed_min_max_width',
array(
'min' => 200,
'max' => 600,
)
);
$width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
$height = max( (int) ceil( $width / 16 * 9 ), 200 );
$data = array(
'version' => '1.0',
'provider_name' => get_bloginfo( 'name' ),
'provider_url' => get_home_url(),
'author_name' => get_bloginfo( 'name' ),
'author_url' => get_home_url(),
'title' => get_the_title( $post ),
'type' => 'link',
);
$author = get_userdata( $post->post_author );
if ( $author ) {
$data['author_name'] = $author->display_name;
$data['author_url'] = get_author_posts_url( $author->ID );
}
/**
* Filters the oEmbed response data.
*
* @since 4.4.0
*
* @param array $data The response data.
* @param WP_Post $post The post object.
* @param int $width The requested width.
* @param int $height The calculated height.
*/
return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
}
Hooks
- apply_filters( ‘oembed_min_max_width’, array $min_max_width )
-
Filters the allowed minimum and maximum widths for the oEmbed response.
- apply_filters( ‘oembed_response_data’, array $data, WP_Post $post, int $width, int $height )
-
Filters the oEmbed response data.