Last active
January 26, 2021 09:45
-
-
Save abelsaad/f3a189e188710bd0fa283d6e208fc53d to your computer and use it in GitHub Desktop.
WordPress Random name for uploaded files
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 | |
// 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