Clean Up File Names on Upload
Converts uploaded file names to lowercase, removes accents and replaces spaces and symbols with dashes, for tidy and portable URLs.
Opens WPIDE on
Code
add_filter( 'sanitize_file_name', function ( $filename ) {
$info = pathinfo( $filename );
$extension = isset( $info['extension'] ) && '' !== $info['extension'] ? '.' . strtolower( $info['extension'] ) : '';
$name = isset( $info['filename'] ) ? $info['filename'] : $filename;
$name = remove_accents( $name );
$name = strtolower( preg_replace( '/[^A-Za-z0-9]+/', '-', $name ) );
$name = trim( $name, '-' );
return ( '' === $name ? 'file' : $name ) . $extension;
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.