I used custom post type, where ‘thumbnail’, ‘medium’ and ‘large’ sized not required. I need to disable this sizes and create function or plugin, where i can set, which image size is required for each custom post type.
My first step is hooking of get_intermediate_image_sizes function from wp-includes/media.php. I have added this code to functions.php but it not working 🙁
add_filter('get_intermediate_image_sizes', 'get_intermediate_image_sizes_fixed');
function get_intermediate_image_sizes_fixed() {
global $_wp_additional_image_sizes;
//$image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
$image_sizes = array();
if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
$image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
return apply_filters( 'intermediate_image_sizes', $image_sizes );
}
UPD: Editing this line in media.php is working. Default types after changes not generating.
$image_sizes = array('thumbnail', 'medium', 'large');
But how to make work my hook?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think the only solution you have at the moment is to disable all intermediate image sizes:
add_filter( 'intermediate_image_sizes', '__return_empty_array', 99 );
And then manually generate them, depending on the post type, by hooking into ‘wp_generate_attachment_metadata’, where you do have access to the attachment id (and therefore to it’s parent post):
function do_your_stuff( $data, $attachment_id ) {
// generate intermediate images
return $data;
}
add_filter( 'wp_generate_attachment_metadata', 'do_your_stuff', 10, 2 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0