This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
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.
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!
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.