函数文档

get_oembed_response_data_rich()

💡 云策文档标注

概述

get_oembed_response_data_rich() 是一个 WordPress 过滤器函数,用于修改 oEmbed 响应数据,以返回包含 iframe 嵌入代码的富媒体响应。它处理宽度、高度、类型和缩略图信息。

关键要点

  • 函数作用:过滤 oEmbed 响应数据,生成富媒体(rich)类型的响应,包含 iframe 嵌入代码。
  • 参数:接受 $data(响应数据数组)、$post(WP_Post 对象)、$width(请求宽度)、$height(计算高度)四个必需参数。
  • 返回值:返回修改后的响应数据数组,包含 width、height、type、html 等字段。
  • 缩略图处理:根据帖子类型和附件状态,自动添加缩略图 URL、宽度和高度到响应数据中。
  • 相关函数:涉及 get_post_embed_html()、wp_attachment_is()、has_post_thumbnail() 等多个 WordPress 核心函数。

代码示例

function get_oembed_response_data_rich( $data, $post, $width, $height ) {
    $data['width']  = absint( $width );
    $data['height'] = absint( $height );
    $data['type']   = 'rich';
    $data['html']   = get_post_embed_html( $width, $height, $post );

    // Add post thumbnail to response if available.
    $thumbnail_id = false;

    if ( has_post_thumbnail( $post->ID ) ) {
        $thumbnail_id = get_post_thumbnail_id( $post->ID );
    }

    if ( 'attachment' === get_post_type( $post ) ) {
        if ( wp_attachment_is_image( $post ) ) {
            $thumbnail_id = $post->ID;
        } elseif ( wp_attachment_is( 'video', $post ) ) {
            $thumbnail_id = get_post_thumbnail_id( $post );
            $data['type'] = 'video';
        }
    }

    if ( $thumbnail_id ) {
        list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 0 ) );
        $data['thumbnail_url']                                      = $thumbnail_url;
        $data['thumbnail_width']                                    = $thumbnail_width;
        $data['thumbnail_height']                                   = $thumbnail_height;
    }

    return $data;
}

注意事项

  • 该函数在 WordPress 4.4.0 版本中引入,用于增强 oEmbed 功能。
  • 处理附件类型时,会根据是否为图像或视频调整缩略图和响应类型。
  • 使用 absint() 确保宽度和高度为非负整数,避免潜在错误。

📄 原文内容

Filters the oEmbed response data to return an iframe embed code.

Parameters

$dataarrayrequired
The response data.
$postWP_Postrequired
The post object.
$widthintrequired
The requested width.
$heightintrequired
The calculated height.

Return

array The modified response data.

Source

function get_oembed_response_data_rich( $data, $post, $width, $height ) {
	$data['width']  = absint( $width );
	$data['height'] = absint( $height );
	$data['type']   = 'rich';
	$data['html']   = get_post_embed_html( $width, $height, $post );

	// Add post thumbnail to response if available.
	$thumbnail_id = false;

	if ( has_post_thumbnail( $post->ID ) ) {
		$thumbnail_id = get_post_thumbnail_id( $post->ID );
	}

	if ( 'attachment' === get_post_type( $post ) ) {
		if ( wp_attachment_is_image( $post ) ) {
			$thumbnail_id = $post->ID;
		} elseif ( wp_attachment_is( 'video', $post ) ) {
			$thumbnail_id = get_post_thumbnail_id( $post );
			$data['type'] = 'video';
		}
	}

	if ( $thumbnail_id ) {
		list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 0 ) );
		$data['thumbnail_url']                                      = $thumbnail_url;
		$data['thumbnail_width']                                    = $thumbnail_width;
		$data['thumbnail_height']                                   = $thumbnail_height;
	}

	return $data;
}

Changelog

Version Description
4.4.0 Introduced.