Skip to content

Instantly share code, notes, and snippets.

@jeonghwan-kim
Last active January 11, 2016 12:02
Show Gist options
  • Save jeonghwan-kim/d05212a80d300a21b481 to your computer and use it in GitHub Desktop.
Save jeonghwan-kim/d05212a80d300a21b481 to your computer and use it in GitHub Desktop.
<?php
function sanitize_filename_on_upload($filename) {
$ext = end(explode('.', $filename));
// Replace all weird characters
$sanitized = preg_replace('/[^a-zA-Z0-9-_.]/', '_', substr($filename, 0, -(strlen($ext)+1)));
// Replace dots inside filename
$sanitized = str_replace('.', '-', $sanitized);
return strtolower($sanitized . '.' . $ext);
}
add_filter('sanitize_file_name', 'sanitize_filename_on_upload', 10);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment