Skip to content

Instantly share code, notes, and snippets.

@abelsaad
Last active January 26, 2021 09:45
Show Gist options
  • Save abelsaad/f3a189e188710bd0fa283d6e208fc53d to your computer and use it in GitHub Desktop.
Save abelsaad/f3a189e188710bd0fa283d6e208fc53d to your computer and use it in GitHub Desktop.
WordPress Random name for uploaded files
<?php
// randomize upload filenames
function htg_randomize_uploaded_filename( $filename ) {
// does it have an extension? grab it
$ext = empty( pathinfo( $filename )['extension'] ) ? '' : '.' . pathinfo( $filename )['extension'];
// return the first 8 characters of the MD5 hash of the name, along with the extension
return substr(md5($filename), 0, 8) . $ext;
}
add_filter( 'sanitize_file_name', 'htg_randomize_uploaded_filename', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment