WordPress: Remove Special Characters From Uploaded Files


During my experience with WordPress I have found out users who are not familiar with Internet, tends to write filenames with special characters and even white spaces. It is almost impossible to teach them not to do that because they have been doing it all their life.

The problem with accents and WordPress appears when you are using URL Rewriting and your rules don’t consider special Latin characters. You will get a Not Found message when trying to access these files.

WordPress do not automatically remove Latin characters like accents (á, é, í, ó, ú), but it comes with a very handy internal function called remove_accents. So, if you want to make sure Latin characters are removed from file name when uploaded, you can use this in your functions.php file:

add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);
function sa_sanitize_spanish_chars ($filename) {
return remove_accents( $filename );
}

The remove_accents() function is located in wp-includes/formatting.php.