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。

檢索一個特定文章的嵌入程式碼。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
}
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 ); }
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
檢視更多 >