Skip to content

Instantly share code, notes, and snippets.

@joho1968
Created November 13, 2025 07:45
Show Gist options
  • Select an option

  • Save joho1968/a888bf3a5d1a38e6ccafef8b9ca151eb to your computer and use it in GitHub Desktop.

Select an option

Save joho1968/a888bf3a5d1a38e6ccafef8b9ca151eb to your computer and use it in GitHub Desktop.
Modifying the behavior of WordPress' sanitize_file_name()
<?php
/**
* If you're using the WordPress function sanitize_file_name() and want
* to use it with filenames like my-custom.min.css, you'll quickly find
* out that .min.css isn't accepted as a valid extension.
*
* Here's a quick fix, using WordPress' filtering mechanisms.
*
* Joaquim Homrighausen <[email protected]>
* Nov 13, 2025
*/
function allow_min_css_extension( array $mime_types ) : array {
// because of how it's parsed, ['min.css'] does not work here
$mime_types['min'] = 'text/css';
return( $mime_types );
}
add_filter( 'mime_types', 'allow_min_css_extension', 10, 1 );
$my_filename = sanitize_file_name( wp_unslash( ( $my_filename ) ) );
remove_filter( 'mime_types', 'docsmatter_allow_min_css_extension', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment