Created
March 27, 2017 04:19
-
-
Save davewoodhall/f19cd26a4cd39aaee2a026cffb46eedc to your computer and use it in GitHub Desktop.
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 | |
// I like to verify if a function exists just because anyone (including yourself) | |
// could screw up the file accidently so always verify, so my functions are wrapped | |
// inside a if(function_exists('function_name')) condition | |
// Replace wierd non-URL-safe characters | |
if(!function_exists('dw_replaceChars')) { | |
function dw_replaceChars($str) { | |
// List all wierd characters and match with what we want | |
$unwanted_array = array( | |
'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'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', 'þ'=>'b', 'ÿ'=>'y' ); | |
// Tell PHP to match them all and do the proper replacements | |
$str = strtr( $str, $unwanted_array ); | |
// Return value | |
return $str; | |
} | |
} | |
// URL-safe renaming functionality | |
if(!function_exists('dw_url')) { | |
function dw_url( $str = '' ) { | |
// Again, if we've included the previous function, replace the characters | |
if(function_exists('dw_replaceChars')) { | |
$str = dw_replaceChars( $str ); | |
} | |
// Convert everything to lower case | |
$str = strtolower($str); | |
// Replace everything not letters (a-z), numbers (0-9), periods or hyphens to nothing | |
$str = preg_replace("/[^a-z0-9.-]/", "", $str); | |
// Replace any spaces with hyphens | |
$str = preg_replace("/[\s-]+/", "-", $str); | |
// Return value | |
return $str; | |
} | |
} | |
// Tell WP to deal with the file names | |
if(!function_exists('dw_safeFileUploads')) { | |
// Parameter $file is the uploaded file (managed by WP) | |
function dw_safeFileUploads( $file ) { | |
// Call the function that corrects the file name | |
// No need to call other functions as this does all it needs | |
$file['name'] = dw_url( $file['name'] ); | |
// Return value and use this as the file name | |
return $file; | |
} | |
// WP has a hook that combined functions and calls them when needed | |
// Tell WP to use our custom functions | |
add_action('wp_handle_upload_prefilter', 'dw_safeFileUploads'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment