admin_post_thumbnail_html │ filter-hook │ WP 2.9.0

Allows you to change the HTML (content) of the Featured Image meta box.

The filter fires only when the classic post editor is used, not Gutenberg.

Usage

add_filter( 'admin_post_thumbnail_html', 'wp_kama_admin_post_thumbnail_html_filter', 10, 3 );

/**
 * Function for `admin_post_thumbnail_html` filter-hook.
 * 
 * @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 string
 */
function wp_kama_admin_post_thumbnail_html_filter( $content, $post_id, $thumbnail_id ){
	// filter...
	return $content;
}
$content(string)
HTML content of the Featured Image meta box.
$post_id(int)
Post ID.
$thumbnail_id(int/null)
Attachment ID (an image in the Media Library), or null when the post has no featured image.

Examples

#1 Example contents of the $content variable

No thumbnail:

<p class="hide-if-no-js">
	<a href="https://wp-test.com/wp-admin/media-upload.php?post_id=106&type=image&TB_iframe=1" id="set-post-thumbnail" class="thickbox">
		Set featured image
	</a>
</p>

<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="-1" />

Thumbnail set:

<p class="hide-if-no-js">
	<a href="https://example.com/wp-admin/media-upload.php?post_id=106&type=image&TB_iframe=1" id="set-post-thumbnail" aria-describedby="set-post-thumbnail-desc" class="thickbox">
		<img width="580" height="580" src="https://example.com/wp-content/uploads/2020/12/image.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="" loading="lazy" srcset="https://example.com/wp-content/uploads/2020/12/image.jpg 700w, https://example.com/wp-content/uploads/2020/12/image-300x300.jpg 300w, https://example.com/wp-content/uploads/2020/12/image-150x150.jpg 150w" sizes="(max-width: 580px) 100vw, 580px" />
	</a>
</p>

<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">
	Click the image to edit or update it
</p>

<p class="hide-if-no-js">
	<a href="#" id="remove-post-thumbnail">Remove featured image</a>
</p>

<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="100" />

#2 Add any content to the beginning or end of the meta box

add_filter( 'admin_post_thumbnail_html', function ( $content ) {
	$before_content = '<p>Content at the beginning.</p>';
	$after_content  = '<p>Content at the end.</p>';

	return $before_content . $content . $after_content;
} );

#3 Add a notice when the thumbnail is not an SVG

function admin_post_thumbnail_html_add_notice( $content, $post_id, $thumbnail_id ) {
	if ( ! ( $thumbnail_id && 'school' === get_post_type( $post_id ) ) ) {
		return $content;
	}

	$path = get_attached_file( $thumbnail_id );

	if ( ! str_contains( $path, '.svg' ) ) {
		$text    = 'The selected logo is not in SVG format! It will not appear on the school page or in the school list.';
		$notice  = '<div style="background: #ff0000;padding: 5px;color: #ffffff; font-weight: bold;">' . $text . '</div>';
		$content = $notice . $content;
	}

	return $content;
}

add_filter( 'admin_post_thumbnail_html', 'admin_post_thumbnail_html_add_notice', 11, 3 );

Changelog

Since 2.9.0 Introduced.
Since 3.5.0 Added the $post_id parameter.
Since 4.6.0 Added the $thumbnail_id parameter.

Where the hook is called

_wp_post_thumbnail_html()
admin_post_thumbnail_html
wp-admin/includes/post.php 1717
return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );

Where the hook is used in WordPress

Usage not found.