Change admin post thumbnail size for custom post type only

I want to change the size of the featued image for one custom post type only. This is what I currently have:

function custom_admin_thumb_size($thumb_size){

    global $post_type;
    if( 'slider' == $post_type ){

        return array(1000,400);

    }else{

        return array(266,266);
    }

}
add_filter( 'admin_post_thumbnail_size', 'custom_admin_thumb_size');

This function does what I expected but I was wondering if there is any better method to call the custom post type “slider” without touching the others.

Thanks in advance

2 Answers
2

admin_post_thumbnail_size takes three parameters:

  1. $thumb_size: selected Thumb Size if you do nothing in the filter.

  2. $thumbnail_id: Thumbnail attachment ID.

  3. $post: associated WP_Post instance

So you may make use of these parameters to have a better control in your CODE. Use the CODE like below:

function custom_admin_thumb_size( $thumb_size, $thumbnail_id, $post ) {
    if( 'slider' === $post->post_type ) {
        return array( 1000, 400 );
    }

    return $thumb_size;
}
add_filter( 'admin_post_thumbnail_size', 'custom_admin_thumb_size', 10, 3);

Leave a Comment