Exciting News! Flipper Code is now WePlugins! Same commitment to WordPress Development excellence, brand new identity.

How to use intermediate_image_sizes_advanced filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
May 6, 2023
5 minutes read

intermediate_image_sizes_advanced filter

Filters the image sizes automatically generated when uploading an image.

To use intermediate_image_sizes_advanced filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

In the below live example, we have defined a function modify_intermediate_image_sizes_advanced_defaults which takes 3 parameters and we registered using add_filter. The first parameter intermediate_image_sizes_advanced is the name of the hook, the second parameter modify_intermediate_image_sizes_advanced_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove intermediate_image_sizes_advanced filter.

Example 1: Disable Specific Upload Sizes

This example demonstrates how to disable specific image sizes when uploading.

    function weplugins_disable_upload_sizes( $sizes, $image_meta ) {
        // Get filetype data.
        $filetype = wp_check_filetype( $image_meta['file'] );

        $exclude_file_types = array(
            'image/gif',
        );

        // Check if file type is on exclude list
        if ( in_array( $filetype['type'], $exclude_file_types ) ) {
            return array();
        }

        return $sizes;
    }
    add_filter( "intermediate_image_sizes_advanced", "weplugins_disable_upload_sizes", 10, 2 );
    

Example 2: Modify Sizes Based on Conditions

In this example, you can modify the $new_sizes variable according to your website requirements.

    function weplugins_modify_intermediate_image_sizes_advanced_defaults($new_sizes, $image_meta, $attachment_id) { 
        // Update the $new_sizes variable and return it.
        return $new_sizes; 
    }
    // Add the filter
    add_filter( "intermediate_image_sizes_advanced", "weplugins_modify_intermediate_image_sizes_advanced_defaults", 10, 3 );
    

Example 3: Remove a Hook Callback

To remove a previously registered hook callback, see the example below.

    remove_filter( "intermediate_image_sizes_advanced", "weplugins_modify_intermediate_image_sizes_advanced_defaults", 10, 3 );
    

Please make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

Below are the 3 parameters required to use this hook:

  • $new_sizes : (array) Associative array of image sizes to be created.
  • $image_meta : (array) The image meta data: width, height, file, sizes, etc.
  • $attachment_id : (int) The attachment post ID for the image.

Access Premium WordPress Plugins

If you’re having any trouble using this hook or need customization, please Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.