Skip to content

Instantly share code, notes, and snippets.

@jeffreyd00
Forked from jnicol/functions.php
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreyd00/a06e516411771aa1a5ae to your computer and use it in GitHub Desktop.
Save jeffreyd00/a06e516411771aa1a5ae to your computer and use it in GitHub Desktop.
Sanitize WordPress filenames on upload
/**
* WordPress allows UTF8 characters such as copyright symbol in filenames but these break in Safari
*
* @see https://wordpress.org/support/topic/uploaded-image-with-accents-in-name-image-dont-show-in-safari-6 for original function
* @see https://core.trac.wordpress.org/ticket/22363 for progress on fixing this bug
*
* #wordpress
*/
function sanitize_filename_on_upload($filename) {
$ext = end(explode('.',$filename));
$sanitized = preg_replace('/[^a-zA-Z0-9-_.]/','', substr($filename, 0, -(strlen($ext)+1)));
$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