-
-
Save tazeverywhere/fc0540cc55be294985b757d8086c0675 to your computer and use it in GitHub Desktop.
Clean file name when uploading in WordPress
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 if (!defined('ABSPATH')) die('Restricted Area'); | |
/* | |
* Plugin Name: Sanitize File Name | |
* Description: Clean file name when uploading files in WordPress. | |
* Version: 20180218 | |
* Author: Mickaël Gris (Neticpro) | |
* Author URI: https://wpchannel.com/renommer-automatiquement-fichiers-accentues-wordpress/ | |
*/ | |
function wpc_sanitize_french_chars($filename) { | |
/* Force the file name in UTF-8 (encoding Windows / OS X / Linux) */ | |
$filename = mb_convert_encoding($filename, "UTF-8"); | |
$char_not_clean = array('/À/','/Á/','/Â/','/Ã/','/Ä/','/Å/','/Ç/','/È/','/É/','/Ê/','/Ë/','/Ì/','/Í/','/Î/','/Ï/','/Ò/','/Ó/','/Ô/','/Õ/','/Ö/','/Ù/','/Ú/','/Û/','/Ü/','/Ý/','/à/','/á/','/â/','/ã/','/ä/','/å/','/ç/','/è/','/é/','/ê/','/ë/','/ì/','/í/','/î/','/ï/','/ð/','/ò/','/ó/','/ô/','/õ/','/ö/','/ù/','/ú/','/û/','/ü/','/ý/','/ÿ/', '/©/'); | |
$clean = array('a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','u','u','u','u','y','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','o','u','u','u','u','y','y','copy'); | |
$friendly_filename = preg_replace($char_not_clean, $clean, $filename); | |
/* After replacement, we destroy the last residues */ | |
$friendly_filename = utf8_decode($friendly_filename); | |
$friendly_filename = preg_replace('/\?/', '', $friendly_filename); | |
/* Lowercase */ | |
$friendly_filename = strtolower($friendly_filename); | |
return $friendly_filename; | |
} | |
add_filter('sanitize_file_name', 'wpc_sanitize_french_chars', 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment