Created
May 2, 2018 21:34
-
-
Save kierzniak/a80607488ad0954b66d182ea65108ea3 to your computer and use it in GitHub Desktop.
Sanitize filename on WordPress upload to not brake links with UTF-8 characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sanitize filename to not brake links with UTF-8 characters | |
* | |
* WordPress allow to upload files with names containing UTF-8 characters. Some | |
* browsers do not handle properly UTF-8 characters in url which causes 404 errors. | |
* This filter will remove UTF-8 characters from filename before saving it. | |
* | |
* @see https://core.trac.wordpress.org/ticket/22363 Bug request | |
* | |
* @param string $filename Filename | |
* | |
* @return string Sanitized filename | |
*/ | |
function sanitize_filename( $filename ) { | |
$file_parts = explode( '.', $filename ); | |
$extension = array_pop( $file_parts ); | |
$filename = sanitize_title( preg_replace( '/[^A-Za-z0-9\-]/', '', join( '.', $file_parts ) ) ); | |
return sprintf('%s.%s', $filename, $extension); | |
} | |
add_filter( 'sanitize_file_name', 'sanitize_filename' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment