函数文档

get_post_embed_html()

💡 云策文档标注

概述

get_post_embed_html() 函数用于获取特定文章的嵌入代码,生成包含 iframe 和脚本的 HTML 输出。它接受宽度、高度和文章参数,并返回嵌入代码字符串或 false。

关键要点

  • 参数:$width(int,必需,响应宽度)、$height(int,必需,响应高度)、$post(int|WP_Post,可选,文章 ID 或对象,默认为全局 $post)。
  • 返回值:成功时返回嵌入代码字符串,文章不存在时返回 false。
  • 功能:通过 get_post_embed_url() 获取嵌入 URL,添加安全密钥,构建 iframe 和脚本标签。
  • 过滤器:使用 apply_filters('embed_html', $output, $post, $width, $height) 过滤输出。
  • 注意事项:脚本必须放置在 iframe 和 secret 之后,以避免 wp_filter_oembed_result() 中的正则解析问题。

代码示例

function get_post_embed_html( $width, $height, $post = null ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    $embed_url = get_post_embed_url( $post );

    $secret     = wp_generate_password( 10, false );
    $embed_url .= "#?secret={$secret}";

    $output = sprintf(
        '%3$s',
        esc_attr( $secret ),
        esc_url( get_permalink( $post ) ),
        get_the_title( $post )
    );

    $output .= sprintf(
        '',
        esc_url( $embed_url ),
        absint( $width ),
        absint( $height ),
        esc_attr(
            sprintf(
                /* translators: 1: Post title, 2: Site title. */
                __( '“%1$s” — %2$s' ),
                get_the_title( $post ),
                get_bloginfo( 'name' )
            )
        ),
        esc_attr( $secret )
    );

    $js_path = '/js/wp-embed' . wp_scripts_get_suffix() . '.js';
    $output .= wp_get_inline_script_tag(
        trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "
//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
    );

    return apply_filters( 'embed_html', $output, $post, $width, $height );
}

注意事项

由于 wp_filter_oembed_result() 中的正则表达式问题,脚本标签必须放置在 iframe 和 secret 之后,以确保兼容性,此问题可追溯至 WordPress 4.4。


📄 原文内容

Retrieves the embed code for a specific post.

Parameters

$widthintrequired
The width for the response.
$heightintrequired
The height for the response.
$postint|WP_Postoptional
Post ID or object. Default is global $post.

Default:null

Return

string|false Embed code on success, false if post doesn’t exist.

Source

function get_post_embed_html( $width, $height, $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$embed_url = get_post_embed_url( $post );

	$secret     = wp_generate_password( 10, false );
	$embed_url .= "#?secret={$secret}";

	$output = sprintf(
		'<blockquote class="wp-embedded-content" data-secret="%1$s"><a href="%2$s">%3$s</a></blockquote>',
		esc_attr( $secret ),
		esc_url( get_permalink( $post ) ),
		get_the_title( $post )
	);

	$output .= sprintf(
		'<iframe sandbox="allow-scripts" security="restricted" src="%1$s" width="%2$d" height="%3$d" title="%4$s" data-secret="%5$s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>',
		esc_url( $embed_url ),
		absint( $width ),
		absint( $height ),
		esc_attr(
			sprintf(
				/* translators: 1: Post title, 2: Site title. */
				__( '“%1$s” — %2$s' ),
				get_the_title( $post ),
				get_bloginfo( 'name' )
			)
		),
		esc_attr( $secret )
	);

	/*
	 * Note that the script must be placed after the <blockquote> and <iframe> due to a regexp parsing issue in
	 * `wp_filter_oembed_result()`. Because of the regex pattern starts with `|(<blockquote>.*?</blockquote>)?.*|`
	 * wherein the <blockquote> is marked as being optional, if it is not at the beginning of the string then the group
	 * will fail to match and everything will be matched by `.*` and not included in the group. This regex issue goes
	 * back to WordPress 4.4, so in order to not break older installs this script must come at the end.
	 */
	$js_path = '/js/wp-embed' . wp_scripts_get_suffix() . '.js';
	$output .= wp_get_inline_script_tag(
		trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "n//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
	);

	/**
	 * Filters the embed HTML output for a given post.
	 *
	 * @since 4.4.0
	 *
	 * @param string  $output The default iframe tag to display embedded content.
	 * @param WP_Post $post   Current post object.
	 * @param int     $width  Width of the response.
	 * @param int     $height Height of the response.
	 */
	return apply_filters( 'embed_html', $output, $post, $width, $height );
}

Hooks

apply_filters( ’embed_html’, string $output, WP_Post $post, int $width, int $height )

Filters the embed HTML output for a given post.

Changelog

Version Description
4.4.0 Introduced.