get_post_embed_html

函数
get_post_embed_html ( $width, $height, $post = null )
参数
  • (int) $width The width for the response.
    Required:
  • (int) $height The height for the response.
    Required:
  • (int|WP_Post) $post Optional. Post ID or object. Default is global `$post`.
    Required:
    Default: null
返回值
  • (string|false) Embed code on success, false if post doesn't exist.
定义位置
相关方法
get_post_embed_urlget_post_metaget_post_modified_timeget_embed_templateget_post_to_edit
引入
4.4.0
弃用
-

get_post_embed_html函数用于检索文章嵌入内容的html代码。它只接受一个参数,即文章的ID: 此函数返回显示文章嵌入内容所需的HTML代码。嵌入的内容可以是从视频到音频文件或社交媒体文章的任何内容: 该函数接受一个参数,即要检索其嵌入内容的文章的ID。

检索一个特定文章的嵌入代码。

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.
	$output .= wp_get_inline_script_tag(
		file_get_contents( ABSPATH . WPINC . '/js/wp-embed' . wp_scripts_get_suffix() . '.js' )
	);

	/**
	 * 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 );
}

常见问题

FAQs
查看更多 >