Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active July 11, 2017 15:06
Show Gist options
  • Save luizbills/cad722f6d9ea5267a8d4e0da2afe62a8 to your computer and use it in GitHub Desktop.
Save luizbills/cad722f6d9ea5267a8d4e0da2afe62a8 to your computer and use it in GitHub Desktop.
Produces cleaner filenames for uploads
<?php
/**
* Produces cleaner filenames for uploads
*
* @author WP Artisan https://wpartisan.me/
*
* @param string $filename
* @return string
*/
add_filter( 'sanitize_file_name', 'wpartisan_sanitize_file_name', 10, 1 );
function wpartisan_sanitize_file_name( $filename ) {
$sanitized_filename = remove_accents( $filename ); // Convert to ASCII
// Standard replacements
$invalid = array(
' ' => '-',
'%20' => '-',
'_' => '-',
);
$sanitized_filename = str_replace( array_keys( $invalid ), array_values( $invalid ), $sanitized_filename );
$sanitized_filename = preg_replace('/[^A-Za-z0-9-\. ]/', '', $sanitized_filename); // Remove all non-alphanumeric except .
$sanitized_filename = preg_replace('/\.(?=.*\.)/', '', $sanitized_filename); // Remove all but last .
$sanitized_filename = preg_replace('/-+/', '-', $sanitized_filename); // Replace any more than one - in a row
$sanitized_filename = str_replace('-.', '.', $sanitized_filename); // Remove last - if at the end
$sanitized_filename = strtolower( $sanitized_filename ); // Lowercase
return $sanitized_filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment