Remove Accents form file uploads (sanitize)


#1

I found out while using the Envira Gallery that files that where uploaded did get an “-” instead of an “Space”

So we got filenames like: This-File-Name.jpg instead of This File Name.jpg

We would not want that, so went looking for a solution.
Envira could not help me since it was Jupiter related.

With support i came at this code:

/*
 * To remove accents from the name of uploaded files
 * Thanks to: https://goo.gl/h2tYrZ
 */
if ( ! function_exists( 'mk_sanitize_file_uploads' ) ) {

	function mk_sanitize_file_uploads( $file ) {
		$file['name'] = sanitize_file_name( $file['name'] );
		$file['name'] = preg_replace( '/[^a-zA-Z0-9\_\-\.]/', '', $file['name'] );
		$file['name'] = strtolower( $file['name'] );
		add_filter( 'sanitize_file_name', 'remove_accents' );

		return $file;
	}
	add_filter( 'wp_handle_upload_prefilter', 'mk_sanitize_file_uploads' );

}

This is in the file: wp-content/themes/jupiter/framework/functions/general-functions.php

We wanted to remove the Sanitize Function.
So we added this code:

//Remove Jupiter Sanitize file name function from the parent theme
function remove_parent_filters(){ //Have to do it after theme setup, because child theme functions are loaded first
	remove_filter('wp_handle_upload_prefilter', 'mk_sanitize_file_uploads');
}
add_action( 'after_setup_theme', 'remove_parent_filters' );

To our Jupiter-Child theme in the file functions.php

Now we can upload files with nice names again.