Skip to content

Instantly share code, notes, and snippets.

@diegosomar
Last active October 19, 2023 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegosomar/bbd408b17d0ee6904f4b94111d3eb2fb to your computer and use it in GitHub Desktop.
Save diegosomar/bbd408b17d0ee6904f4b94111d3eb2fb to your computer and use it in GitHub Desktop.
Change filename for Wordpress uploads
<?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