Skip to content

Instantly share code, notes, and snippets.

@ekandreas
Created March 21, 2019 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekandreas/839136c205c999055acf954cf15fd4f7 to your computer and use it in GitHub Desktop.
Save ekandreas/839136c205c999055acf954cf15fd4f7 to your computer and use it in GitHub Desktop.
fixa fula filnamn wordpress frozzare style
<?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