Rename uploaded files in multiple upload
-
Hi,
I’ve created a form where users can upload up to 3 pictures along with a required Name Surname field.
I have found a past discussion in which it is suggested to use a custom mu-plugin to accomplish that. I would like to append the Name and Surname contained in “name-1” to each of the uploaded files names, replacing spaces with _ (underscore)
this is the code I am trying to use:
<?php function wpmudev_modify_uploaded_file_names( $filename, $filename_raw ) { $info = pathinfo($filename); $ext = empty($info['extension']) ? '' : '.' . $info['extension']; if ( !empty( $_POST['name-1'] ) ) { $user_name = sanitize_text_field( $_POST['name-1'] ); $filename = $user_name . '_' . basename($filename, $ext) . $ext; // Append user_name before the original filename $filename = str_replace( ' ', '_', $filename ); } return $filename; } add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_check', 10, 1 ); function wpmudev_uploaded_filename_check( $form_id ) { if( $form_id == 202751 ) { add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2); } }
the expected result is:
name-1=John Doe
Uploaded files: photo1.jpg, photo2.jpg, photo3.jpg
Renamed files: John_Doe_photo1.jpg, John_Doe_photo2.jpg, John_Doe_photo3.jpgCan you help me to make it work?
The page I need help with: [log in to see the link]
- The topic ‘Rename uploaded files in multiple upload’ is closed to new replies.