Created
March 21, 2019 10:02
-
-
Save ekandreas/839136c205c999055acf954cf15fd4f7 to your computer and use it in GitHub Desktop.
fixa fula filnamn wordpress frozzare style
This file contains hidden or 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 | |
add_filter( 'wp_handle_upload_prefilter', function ( $file ) { | |
if ( ! is_array( $file ) && ! is_string( $file ) ) { | |
return $file; | |
} | |
if ( ! is_array( $file ) ) { | |
$file = [ | |
'name' => $file, | |
]; | |
} | |
$search = [ '/[^a-zA-Z0-9 \.\&\/_-]+/', '/[ \.\&\/-]+/' ]; | |
$replace = [ '', '-' ]; | |
$path = pathinfo( $file['name'] ); | |
$filename = preg_replace( '/.' . $path['extension'] . '$/', '', $file['name'] ); | |
$filename = html_entity_decode( $filename, ENT_QUOTES, 'UTF-8' ); | |
$filename = remove_accents( $filename ); | |
$filename = preg_replace( $search, $replace, $filename ); | |
$filename = trim( $filename, '-' ); | |
$filename = strtolower( $filename ); | |
$file['name'] = $filename . '.' . $path['extension']; | |
return $file; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment