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:
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name' );
function custom_upload_name( $file )
$file['name'] = 'dontstealmypicture-'.$file['name'];
This could became a tiny plugin, or either it could go to the theme’s functions.php, it’s up to you.