如何限制wp生成附件元数据()到某些中间图像大小

In Wordpress, we have added our own intermediate image sizes to the standard Wordpress sizes using add_image_size().

Using our own admin interface to upload images, we then use wp_generate_attachment_metadata() to create all thumbnails and save them to a predefined folder on the server.

However, we would like to restrict wp_generate_attachment_metadata() to generate our custom defined image sizes ONLY and ignore the Wordpress standard sizes.

Is this possible?

Thanks in advance for any help!

It looks like there is a filter for the $sizes array generated when using wp_generate_attachement_metadata() called intermediate_image_sizes_advanced.

I found this snippet that should do the trick if you put this in a functions file of some sort (normally it would probably go in your functions.php file):

/**
 * Snippet Name: Disable auto creating of image sizes
 * Snippet URL: http://www.wpcustoms.net/snippets/disable-auto-creating-image-sizes/
 */
 function wpc_unset_imagesizes($sizes){
    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['medium_large']);
    unset( $sizes['large']);
}
add_filter('intermediate_image_sizes_advanced', 'wpc_unset_imagesizes' );

The above snippet should remove all of the default Wordpress image sizes from the $sizes array used in wp_generate_attachment_metadata() and only generate any custom image sizes you have added.

NOTE: I have not tested this code, but it looks straight forward to me. If you want the filter to only effect your "own admin interface" you may not want to put the snippet in a separate functions file that only gets called in your admin interface.

See the developer reference for the function and the hook below: https://developer.wordpress.org/reference/functions/wp_generate_attachment_metadata/ https://developer.wordpress.org/reference/hooks/intermediate_image_sizes_advanced/