wppaste
WordPress

_wp_post_thumbnail_html( int|null $thumbnail_id = null, int|WP_Post|null $post = null ): string

Since
2.9.0
Source
wp-admin/includes/post.php:1642
Returns HTML for the post thumbnail meta box.

Compatibility

WordPress
since 2.9.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

$thumbnail_idint|nulloptional
Thumbnail attachment ID. Default null.Default: null
$postint|WP_Post|nulloptional
The post ID or object associated with the thumbnail. Defaults to global $post.Default: null

Return value

string
The post thumbnail HTML.

Performance profile

How much work a call to _wp_post_thumbnail_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

Reaches the database via get_post().

Scaling
Constant

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

Instructions
56–105

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

Plugin surface
2 hooks

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

Called by
3

3 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

Further down the call graph this can also reach option, 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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
always56–57wp_get_additional_image_sizes(), get_post(), get_post_type_object(), get_upload_iframe_src(), esc_url(), esc_html(), sprintf(), esc_attr(), apply_filters()
!get_post()60–61wp_get_additional_image_sizes(), get_post(), get_post_type_object(), get_upload_iframe_src(), esc_url(), esc_html(), sprintf(), get_post(), esc_attr(), apply_filters()
get_post() && empty($thumbnail_html)78–80wp_get_additional_image_sizes(), get_post(), get_post_type_object(), get_upload_iframe_src(), esc_url(), esc_html(), sprintf(), get_post(), apply_filters(), wp_get_attachment_image(), esc_attr(), apply_filters()
get_post() && !empty($thumbnail_html)103–105wp_get_additional_image_sizes(), get_post(), get_post_type_object(), get_upload_iframe_src(), esc_url(), esc_html(), sprintf(), get_post(), apply_filters(), wp_get_attachment_image(), esc_url(), sprintf(), __(), esc_html(), esc_attr(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 107 instructions, 56–105 executed per call, 5 branches. The work does not change between versions.

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 · 2

2 hooks fire while _wp_post_thumbnail_html() runs, in this order:

  1. apply_filters( admin_post_thumbnail_size )filterline 1674 (+32 into the body)

    Filters the size used to display the post thumbnail image in the 'Featured image' meta box.

  2. apply_filters( admin_post_thumbnail_html )filterline 1703 (+61 into the body)

    Filters the admin post thumbnail HTML markup to return.

Uses · 10

Used by · 3

Source code

function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {	$_wp_additional_image_sizes = wp_get_additional_image_sizes(); 	$post               = get_post( $post );	$post_type_object   = get_post_type_object( $post->post_type );	$set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox" role="button" aria-haspopup="dialog" aria-controls="wp-media-modal">%s</a></p>';	$upload_iframe_src  = get_upload_iframe_src( 'image', $post->ID ); 	$content = sprintf(		$set_thumbnail_link,		esc_url( $upload_iframe_src ),		'', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.		esc_html( $post_type_object->labels->set_featured_image )	); 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {		$size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 ); 		/**		 * Filters the size used to display the post thumbnail image in the 'Featured image' meta box.		 *		 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'		 * image size is registered, which differs from the 'thumbnail' image size		 * managed via the Settings > Media screen.		 *		 * @since 4.4.0		 *		 * @param string|int[] $size         Requested image size. Can be any registered image size name, or		 *                                   an array of width and height values in pixels (in that order).		 * @param int          $thumbnail_id Post thumbnail attachment ID.		 * @param WP_Post      $post         The post object associated with the thumbnail.		 */		$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post ); 		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size ); 		if ( ! empty( $thumbnail_html ) ) {			$content  = sprintf(				$set_thumbnail_link,				esc_url( $upload_iframe_src ),				' aria-describedby="set-post-thumbnail-desc"',				$thumbnail_html			);			$content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';			$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" role="button">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';		}	} 	$content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />'; 	/**	 * Filters the admin post thumbnail HTML markup to return.	 *	 * @since 2.9.0	 * @since 3.5.0 Added the `$post_id` parameter.	 * @since 4.6.0 Added the `$thumbnail_id` parameter.	 *	 * @param string   $content      Admin post thumbnail HTML markup.	 * @param int      $post_id      Post ID.	 * @param int|null $thumbnail_id Thumbnail attachment ID, or null if there isn't one.	 */	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );}

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 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.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-admin/includes/post.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.