_wp_post_thumbnail_html

Output HTML for the post thumbnail meta-box.

Description

(string) _wp_post_thumbnail_html( (constant) $thumbnail_id = null, (null) $post = null ); 

Returns (string)

html

Parameters (2)

0. $thumbnail_id — Optional. (constant) => null
ID of the attachment used for thumbnail
1. $post — Optional. (null) => null
The post ID or object associated with the thumbnail, defaults to global $post.

Usage

  1. if ( !function_exists( '_wp_post_thumbnail_html' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // ID of the attachment used for thumbnail 
  5. $thumbnail_id = null; 
  6.  
  7. // The post ID or object associated with the thumbnail, defaults to global $post. 
  8. $post = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _wp_post_thumbnail_html($thumbnail_id, $post); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { 
  2. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  3.  
  4. $post = get_post( $post ); 
  5. $post_type_object = get_post_type_object( $post->post_type ); 
  6. $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>'; 
  7. $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID ); 
  8.  
  9. $content = sprintf( $set_thumbnail_link,  
  10. esc_url( $upload_iframe_src ),  
  11. '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise. 
  12. esc_html( $post_type_object->labels->set_featured_image ) 
  13. ); 
  14.  
  15. if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 
  16. $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 ); 
  17.  
  18. /** 
  19. * Filters the size used to display the post thumbnail image in the 'Featured Image' meta box. 
  20. * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' 
  21. * image size is registered, which differs from the 'thumbnail' image size 
  22. * managed via the Settings > Media screen. See the `$size` parameter description 
  23. * for more information on default values. 
  24. * @since 4.4.0 
  25. * @param string|array $size Post thumbnail image size to display in the meta box. Accepts any valid 
  26. * image size, or an array of width and height values in pixels (in that order). 
  27. * If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,  
  28. * default is an array with 266 as both the height and width values. 
  29. * @param int $thumbnail_id Post thumbnail attachment ID. 
  30. * @param WP_Post $post The post object associated with the thumbnail. 
  31. */ 
  32. $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post ); 
  33.  
  34. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size ); 
  35.  
  36. if ( ! empty( $thumbnail_html ) ) { 
  37. $content = sprintf( $set_thumbnail_link,  
  38. esc_url( $upload_iframe_src ),  
  39. ' aria-describedby="set-post-thumbnail-desc"',  
  40. $thumbnail_html 
  41. ); 
  42. $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>'; 
  43. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>'; 
  44.  
  45. $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />'; 
  46.  
  47. /** 
  48. * Filters the admin post thumbnail HTML markup to return. 
  49. * @since 2.9.0 
  50. * @since 3.5.0 Added the `$post_id` parameter. 
  51. * @since 4.6.0 Added the `$thumbnail_id` parameter. 
  52. * @param string $content Admin post thumbnail HTML markup. 
  53. * @param int $post_id Post ID. 
  54. * @param int $thumbnail_id Thumbnail ID. 
  55. */ 
  56. return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );