2011
01.26

Today we got an interesting question in the Hungarian WordPress community forum according to the media library and file upload. One of our members wanted to know, if there is any way to change the filename of the uploaded file. For example insert the site’s name into the filename.

Well, pretty interesting, I must investigate this case.

First I thought i could catch this thing in wp_upload_bits or in some pretty elegant way.
But, I only found a kind of a dirty solution for this problem by rewriting the filename in the $_FILES array just after the upload. So we act like, the user uploaded the file with this name.

This is not as elegant, as it could be, but a working solution. Anyway I share this with you:

1
2
3
4
5
6
7
8
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name' );  

function custom_upload_name( $file )
{
    $file['name'] = 'dontstealmypicture-'.$file['name'];
   
    return $file;
}

This could became a tiny plugin, or either it could go to the theme’s functions.php, it’s up to you.

5 comments so far

Add Your Comment
  1. very helpful. thx.

  2. briliant!

  3. you r my life savior ~

  4. Hello, This code is not working :/ Help me please..

  5. Awesome! Thank you so much for this!!!!