wppaste
WordPress

get_post_embed_html( int $width, int $height, int|WP_Post $post = null ): string|false

Since
4.4.0
Source
wp-includes/embed.php:490
Retrieves the embed code for a specific post.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$widthint
The width for the response.
$heightint
The height for the response.
$postint|WP_Postoptional
Post ID or object. Default is global $post.Default: null

Return value

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

Performance profile

How much work a call to get_post_embed_html() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_get_contents().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–108

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 109.

Plugin surface
1 hook

Third-party callbacks on 'embed_html' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_get_contents()called directly
  • optionoption read or writeget_option()one call below get_post_embed_html()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_embed_html() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9get_post()
always108get_post(), get_post_embed_url(), wp_generate_password(), esc_attr(), get_permalink(), esc_url(), get_the_title(), sprintf(), esc_url(), absint(), absint(), __(), get_the_title(), get_bloginfo(), sprintf(), esc_attr(), esc_attr(), sprintf(), wp_scripts_get_suffix(), file_get_contents(), includes_url(), esc_url_raw(), wp_get_inline_script_tag(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1099–1081
8.51099–1081
8.41099–10812 fewer instructions than PHP 8.3
8.31119–1101
8.21119–1101
8.11119–1101
7.41119–1101

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_post_embed_html() runs, in this order:

  1. apply_filters( embed_html )filterline 547 (+57 into the body)

    Filters the embed HTML output for a given post.

Uses · 15

Show all 15

Used by · 2

Source code

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. */				__( '&#8220;%1$s&#8221; &#8212; %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 );}

Changelog

Introduced in 4.4.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Parameter $post retyped from int|WP_Post to int|WP_Post|null.verified against source
4.4.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/embed.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.