Using WordPress ‘admin_post_thumbnail_size’ PHP filter

The admin_post_thumbnail_size WordPress PHP filter allows you to change the size of the post thumbnail image displayed in the ‘Featured image’ meta box.

Usage

add_filter('admin_post_thumbnail_size', 'your_custom_function', 10, 3);
function your_custom_function($size, $thumbnail_id, $post) {
    // Your custom code here

    return $size;
}

Parameters

  • $size: (string|int[]) Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
  • $thumbnail_id: (int) Post thumbnail attachment ID.
  • $post: (WP_Post) The post object associated with the thumbnail.

More information

See WordPress Developer Resources: admin_post_thumbnail_size

Examples

Change Featured Image Size for a Specific Post Type

This example changes the featured image size for a specific custom post type called ‘product’:

add_filter('admin_post_thumbnail_size', 'change_featured_image_size_for_product', 10, 3);
function change_featured_image_size_for_product($size, $thumbnail_id, $post) {
    if ($post->post_type === 'product') {
        $size = 'medium';
    }
    return $size;
}

This example sets the featured image size to 200×200 pixels:

add_filter('admin_post_thumbnail_size', 'set_featured_image_size_200', 10, 3);
function set_featured_image_size_200($size, $thumbnail_id, $post) {
    $size = array(200, 200);
    return $size;
}

Change Featured Image Size Based on Post Category

This example changes the featured image size based on the post’s category:

add_filter('admin_post_thumbnail_size', 'change_featured_image_size_based_on_category', 10, 3);
function change_featured_image_size_based_on_category($size, $thumbnail_id, $post) {
    $categories = get_the_category($post->ID);
    if (!empty($categories) && $categories[0]->slug === 'news') {
        $size = 'large';
    }
    return $size;
}

This example modifies the featured image size for posts with a specific tag ‘special’:

add_filter('admin_post_thumbnail_size', 'change_featured_image_size_for_special_tag', 10, 3);
function change_featured_image_size_for_special_tag($size, $thumbnail_id, $post) {
    if (has_tag('special', $post->ID)) {
        $size = 'medium_large';
    }
    return $size;
}

This example sets a custom image size called ‘custom-size’ as the featured image size:

add_filter('admin_post_thumbnail_size', 'use_custom_image_size_for_featured_image', 10, 3);
function use_custom_image_size_for_featured_image($size, $thumbnail_id, $post) {
    $size = 'custom-size';
    return $size;
}