I have been trying to rename my images on upload and only piece of code I found was the one that uses a 32char hash.

function make_filename_hash($filename) {
   $info = pathinfo($filename);
   $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
   $name = basename($filename, $ext);
   return md5($name) . $ext;
add_filter('sanitize_file_name', 'make_filename_hash', 10);

My php knowledge is very limited and I looked everywhere for an example that uses some random number being generated instead of a hash, without success. My initial idea was to get the db image id and use it as image filename on upload but I got my brain twisted in the process.

Anyway at this point I would settle for some sort of numbering system that will increase by one digit on every upload or if that is not possbile at least some random number by using a range, like rand(10000,99999).

Any help would be very appreciated thanks a lot


Can you read data from a custom table in functions.php and write to it. I'm trying to read a counter from a table set the image to that counter number and update the counter in the db. It doesn't seem to work, the counter increases but the table never gets updated, and no matter what the value in the db is, the counter does not use that value, so this means it's not reading this value either. I just want to know if there is another way to do this to rename files but with a value from my custom table. This is what I got so far. I was thinking maybe I use the wrong hook but at this point I am a bit stuck. Thanks guys.

 function make_filename_counter($filename) {      
   $fcnt = 0;
   $sql = ("SELECT counter FROM bwbps_filenamecounter WHERE id = 1");
   $cnt = mysql_query($sql);
   foreach($cnt as $cnt) {
      $fcnt = $cnt->counter;
   $fcnt = $fcnt + 1;
   $info = pathinfo($filename);
   $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
   $sql = "UPDATE bwbps_filenamecounter SET counter = " . $fcnt . " WHERE id = 1";
   $qry = mysql_query($sql);
   $fcnt = 0;
  return $fcnt . $ext;
add_filter('sanitize_file_name', 'make_filename_counter', 10); 
  • This and this may have some hints. Update your question if you find useful info there :)
    – brasofilo
    Jul 21, 2012 at 0:28
  • thanks brasofilo but it wasn't what I had in mind. Right now I am stuck with that db issue that i cannot figure out.
    – sky67ro
    Jul 21, 2012 at 22:18
  • I'm not giving to you full asnwer at the moment, but mentioning only, that usage of filter sanitize_file_name is a really bad way to do it. Please keep in mind that this filter is used on lot more cases than just for sanitizing name of uploaded file.
    – D.A.H
    Aug 27, 2020 at 15:57

3 Answers 3


no need to use a custom table, use an option, and the add_attachment hook:

function wpa59168_rename_attachment( $post_ID ) {

    $post = get_post( $post_ID );
    $file = get_attached_file( $post_ID );
    $path = pathinfo( $file );

    $count = get_option( 'wpa59168_counter', 1 );

    // change to $new_name = $count; if you want just the count as filename
    $new_name = $path['filename'] . '_' . $count;
    $new_file = $path['dirname'] . '/' . $new_name . '.' . $path['extension'];
    rename( $file, $new_file );    
    update_attached_file( $post_ID, $new_file );

    update_option( 'wpa59168_counter', $count + 1 );

add_action( 'add_attachment', 'wpa59168_rename_attachment' );
  • thank you very much, this was a great answer and I could use it after a couple of modifications. still I couldn't have done it without your example. thank you
    – sky67ro
    Jul 22, 2012 at 15:30

If you're doing them in numerical order, you could just store them in an option and just increase that option every time you add a file.

  • thank you fdsa, your answer and Milo's really helped me.
    – sky67ro
    Jul 22, 2012 at 15:31

You may check the code of the "Filenames to latin" plugin.

It converts symbols from different languages to latin letters while upload.

The source code of the plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.