_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:1628
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
- Scaling
- Constant
- Instructions
- 56–105
- Plugin surface
- 2 hooks
- Called by
- 3
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.
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.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 56–57 | wp_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–61 | wp_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–80 | wp_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–105 | wp_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:
- apply_filters( admin_post_thumbnail_size )filterline 1660 (+32 into the body)
Filters the size used to display the post thumbnail image in the 'Featured image' meta box.
- apply_filters( admin_post_thumbnail_html )filterline 1689 (+61 into the body)
Filters the admin post thumbnail HTML markup to return.
Uses · 10
- wp_get_additional_image_sizes()Retrieves additional image sizes.
- get_post()Retrieves post data given a post ID or post object.
- get_post_type_object()Retrieves a post type object by name.
- get_upload_iframe_src()Retrieves the upload iframe source URL.
- esc_url()Checks and cleans a URL.
- esc_html()Escaping for HTML blocks.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- __()Retrieves the translation of $text.
- esc_attr()Escaping for HTML attributes.
Used by · 3
- post_thumbnail_meta_box()Displays post thumbnail meta box.
- wp_ajax_get_post_thumbnail_html()Handles retrieving HTML for the featured image via AJAX.
- wp_ajax_set_post_thumbnail()Handles setting the featured image via AJAX.
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">%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">' . 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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.