Skip to content

Instantly share code, notes, and snippets.

@pedrorvidal
Created May 2, 2017 18:05
Show Gist options
  • Select an option

  • Save pedrorvidal/2bed3ede5c8b08c3bf6153e9c27c9882 to your computer and use it in GitHub Desktop.

Select an option

Save pedrorvidal/2bed3ede5c8b08c3bf6153e9c27c9882 to your computer and use it in GitHub Desktop.
WordPress sanitize Filename
/**
* Arquivos acentuados no upload do WordPress
* @url https://wordpress.org/support/topic/uploaded-image-with-accents-in-name-image-dont-show-in-safari-6
*/
function sanitize_filename_on_upload($filename) {
$param = explode('.', $filename);
$ext = end($param);
// 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