I’ve never worked with special characters such as Korean, Arabic or any of the non-english characters, but I’ve been using this and it’s been saving me several headaches. If you guys are willing, feel free to add to it (especially the dw_replaceChars
function below!).
// Santize file uploads
function dw_safeFileUploads( $file ) {
$file['name'] = dw_url( $file['name'] );
return $file;
}
add_action('wp_handle_upload_prefilter', 'dw_safeFileUploads');
// Sanitize permalinks
function dw_cleanPermalink($url){
$url = dw_url( $url );
return $url;
}
add_filter('the_permalink', 'dw_cleanPermalink');
// The grunt work
function dw_url( $str = '' ) {
$str = dw_replaceChars( $str );
$str = strtolower($str);
$str = trim($str);
$str = preg_replace("/[\s-]+/", "-", $str);
$str = str_replace("_", "-",$str);
$str = preg_replace("/[^a-z0-9.-]/", "", $str);
$str = str_replace("-.", ".",$str);
return $str;
}
// Keep the replacement apart just to look cleaner
function dw_replaceChars($str) {
$unwanted_array = array(
' '=>'-', 'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'Ž'=>'Z', 'ž'=>'z', 'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'R'=>'R', 'r'=>'r', "'"=>'-', '"'=>'-', '’'=>'-' );
$str = strtr( $str, $unwanted_array );
return $str;
}