I wrote about a possible workaround at another site, but let me repeat a part of it here with some adjustments to your question:
The Media Library grid view uses a Backbone micro template and fetches the data via ajax.
In the imageSize()
method, in the media-view.js
file, we can see that the medium
size is used and if that size doesn't exists the actual/full size is used instead:
size = size || 'medium';
To use the thumbnail
instead, you can try the following:
/**
* Use a custom fallback image size, if medium size doesn't exists.
*
* @see http://wordpress.stackexchange.com/a/177571/26350
*/
add_filter( 'wp_prepare_attachment_for_js', function( $response, $attachment, $meta )
{
// Fallback image size, if medium size doesn't exists.
$fallback_size = 'thumbnail'; // Edit this to your needs.
// If medium size image doesn't exist:
if( 'image' === $response['type'] && ! isset( $response['sizes']['medium'] ) )
{
// Use the fallback size instead:
if( isset( $response['sizes'][$fallback_size] ) )
$response['sizes']['medium'] = $response['sizes'][$fallback_size];
}
return $response;
}, 10, 3 );
If you want to use other custom fallback image sizes, other than the default ones, you can either add them via the image_size_names_choose
filter or add this to the above check:
$response['sizes']['medium'] = wp_get_attachment_image_src(
$response['id'],
$fallback_size
);
Note that this method replaces the non-exsisting medium image size with the fallback size, instead of the full size, when you insert it into the post content editor.
Another drastic approach would be to override (from a plugin code without modifying the core files) the Backbone micro template to your needs, for example:
<# if ( data.sizes.medium ) { #>
<img src="{{ data.sizes.medium.url }}" draggable="false" alt="" />
<# } else if ( data.sizes.fallback_image_size) { #>
<img src="{{ data.sizes.fallback_image_size.url }}" draggable="false" alt="" />
<# } else { #>
<img src="{{ data.size.url }}" draggable="false" alt="" />
<# } #>
where we replace fallback_image_size
with the fallback image size, e.g. thumbnail
.
But I'm not sure how frequently these micro templates are changing between WordPress versions, so I wouldn't recommend it, for now ;-)