Last active
October 19, 2023 12:53
-
-
Save diegosomar/bbd408b17d0ee6904f4b94111d3eb2fb to your computer and use it in GitHub Desktop.
Change filename for Wordpress uploads
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 | |
/** | |
* Change filename for uploads | |
* | |
* @param string $file | |
*/ | |
add_filter( 'wp_handle_upload_prefilter', function($file){ | |
$hash1 = sha1(mt_rand(10000000,99999999)); | |
$hash2 = substr(md5(rand()), 0, 7); | |
$time = time(); | |
$sanitized_filename = md5( $hash1 . $hash2 . $time ); | |
$info = pathinfo($file['name']); | |
$ext = empty($info['extension']) ? '' : '.' . $info['extension']; | |
$file['name'] = $sanitized_filename . $ext; | |
return $file; | |
}, 999, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment