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.
getimagesize_mimes_to_exts filter
Hey there! If you’re looking to filter the list mapping image mime types to their respective extensions in WordPress, then you’re in the right place. Let’s dive right into how you can use the getimagesize_mimes_to_exts filter.
To use the getimagesize_mimes_to_exts filter, first, you need to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin. At WePlugins, we always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the live examples below, we have defined a function weplugins_modify_getimagesize_mimes_to_exts_defaults which takes one parameter and we registered it using add_filter. The first parameter getimagesize_mimes_to_exts is the name of the hook, the second parameter weplugins_modify_getimagesize_mimes_to_exts_defaults is the name of the function 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 the getimagesize_mimes_to_exts filter.
Parameters
- $mime_to_ext : (array) Array of image mime types and their matching extensions.
Below, the one parameter required to use this hook:
Live Example 1
Here’s how you can use this hook:
function weplugins_modify_getimagesize_mimes_to_exts_defaults($mime_to_ext) { // Update the $mime_to_ext variable according to your website requirements and return this variable. // You can modify the $mime_to_ext variable conditionally too if you want. return $mime_to_ext; } // Add the filter add_filter("getimagesize_mimes_to_exts", "weplugins_modify_getimagesize_mimes_to_exts_defaults", 10, 1);
Live Example 2
To remove a hook callback, use the example below:
remove_filter("getimagesize_mimes_to_exts", "weplugins_modify_getimagesize_mimes_to_exts_defaults", 10, 1);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Live Example 3
If you want to add a new mime type to extensions mapping, you can do it like this:
function weplugins_add_custom_mime_to_ext($mime_to_ext) { $mime_to_ext['image/custom'] = 'custom'; return $mime_to_ext; } // Add the filter add_filter("getimagesize_mimes_to_exts", "weplugins_add_custom_mime_to_ext", 10, 1);
Contact Us
If you’re having any trouble using this hook or need customization, please contact us. We’d be happy to assist you!
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.