Using WordPress ‘admin_post_thumbnail_html’ PHP filter

The admin_post_thumbnail_html WordPress PHP filter allows you to modify the admin post thumbnail HTML markup.

Usage

add_filter('admin_post_thumbnail_html', 'my_custom_admin_post_thumbnail_html', 10, 3);
function my_custom_admin_post_thumbnail_html($content, $post_id, $thumbnail_id) {
    // your custom code here
    return $content;
}

Parameters

  • $content (string) – The existing admin post thumbnail HTML markup.
  • $post_id (int) – The ID of the post.
  • $thumbnail_id (int|null) – The thumbnail attachment ID, or null if there isn’t one.

More information

See WordPress Developer Resources: admin_post_thumbnail_html

Examples

Add a custom CSS class to the post thumbnail

Add a custom CSS class “my-custom-class” to the post thumbnail image in the admin area.

add_filter('admin_post_thumbnail_html', 'add_custom_class_to_admin_thumbnail', 10, 3);
function add_custom_class_to_admin_thumbnail($content, $post_id, $thumbnail_id) {
    $content = str_replace('<img ', '<img class="my-custom-class" ', $content);
    return $content;
}

Change the thumbnail image size

Change the thumbnail image size in the admin area to a custom size (150px by 150px).

add_filter('admin_post_thumbnail_html', 'change_admin_thumbnail_size', 10, 3);
function change_admin_thumbnail_size($content, $post_id, $thumbnail_id) {
    if ($thumbnail_id) {
        $new_thumbnail = wp_get_attachment_image($thumbnail_id, array(150, 150));
        $content = preg_replace('/<img[^>]+>/', $new_thumbnail, $content);
    }
    return $content;
}

Add a watermark to the thumbnail

Add a watermark to the post thumbnail image in the admin area.

add_filter('admin_post_thumbnail_html', 'add_watermark_to_admin_thumbnail', 10, 3);
function add_watermark_to_admin_thumbnail($content, $post_id, $thumbnail_id) {
    $watermark = '<span class="watermark">Sample Watermark</span>';
    $content = $watermark . $content;
    return $content;
}

Display additional information under the thumbnail

Display the image file size and dimensions below the post thumbnail in the admin area.

add_filter('admin_post_thumbnail_html', 'add_image_info_below_thumbnail', 10, 3);
function add_image_info_below_thumbnail($content, $post_id, $thumbnail_id) {
    if ($thumbnail_id) {
        $image_data = wp_get_attachment_metadata($thumbnail_id);
        $file_size = size_format(filesize(get_attached_file($thumbnail_id)), 2);
        $dimensions = "{$image_data['width']}px x {$image_data['height']}px";
        $info = "<p><strong>File size:</strong> {$file_size}</p><p><strong>Dimensions:</strong> {$dimensions}</p>";
        $content .= $info;
    }
    return $content;
}