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

How to use fallback_intermediate_image_sizes filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 3, 2023
5 minutes read

So, you’re diving into WordPress hooks, huh? Great choice! As an Indian developer, I can tell you that hooks are a game-changer in customizing WordPress without touching the core code. Today, let’s explore the fallback_intermediate_image_sizes filter. This is quite handy when you want to filter the image sizes generated for non-image mime types. First things first, to use this hook, you’ll need to register it using add_filter. It’s usually best to do this in a custom WordPress Plugin, so your changes stick around even after a theme update. Remember, the hook takes two parameters: $fallback_sizes and $metadata, which are essential to get things rolling.

Access Premium WordPress Plugins

Example 1: Adding Custom Image Sizes

Here’s a simple example where we add custom image sizes to the fallback sizes array. This is useful if you’re working with unique image dimensions for your project.

  add_filter(
    'fallback_intermediate_image_sizes',
    function ( array $fallback_sizes, array $metadata ) : array {
      return array_merge( $fallback_sizes, array_keys( wp_get_registered_image_subsizes() ) );
    }, 
  10, 2 );
  

Example 2: Modifying Default Fallback Sizes

In this example, we modify the default fallback sizes based on specific requirements. This is the go-to method when you want to tailor the image sizes your website uses.

  function weplugins_modify_fallback_intermediate_image_sizes_defaults($fallback_sizes, $metadata) { 
      // Update the $fallback_sizes variable according to your website requirements and return this variable. 
      // You can modify the $fallback_sizes variable conditionally too if you want.
      return $fallback_sizes; 
  }
  // add the filter
  add_filter( "fallback_intermediate_image_sizes", "weplugins_modify_fallback_intermediate_image_sizes_defaults", 10, 2 );
  

Example 3: Removing a Hook Callback

If you ever need to remove a previously registered hook, here’s how you can do it. Make sure you provide the same callback function name, priority, and number of arguments.

  remove_filter( "fallback_intermediate_image_sizes", "weplugins_modify_fallback_intermediate_image_sizes_defaults", 10, 2 );
  

If you’re having any trouble using this hook or need custom development, feel free to contact us. Our team at WePlugins is always here to help!

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.