Menu Close

Changes the uploaded image back to original file name elementor forms

Elementor Forms - Changes the uploaded image back to original file name, change File_ID = form Id

/ Changes the uploaded image back to original file name, change File_ID = form Id

/**
* @param string $filename Unique file name.
* @param string $ext File extension, eg. “.png”.
* @param string $dir Directory path.
* @param callable|null $unique_filename_callback Callback function that generates the unique file name.
*/
add_filter( ‘wp_unique_filename’, function( $filename, $ext, $dir, $unique_filename_callback ) {
$FORM_FIELD_FILE_ID = ‘image_upload’;

static $file_count = 0; //used to get the current file when multiple uploads in a form

// first make sure its an upload via elementor forms
// or you can once again only add this filter hook if you know that this is
// a desired form as in the above example
if ( false === strpos( $dir, ‘/elementor/forms’ ) ) {
return $filename;
}

// get the original file name
$original_filename = $_FILES[‘form_fields’][$FORM_FIELD_FILE_ID][$file_count][‘name’];

// Bump counter for next file
$file_count++;
// Return your desired file name (original_filename)
return $original_filename;
}, 10, 4);

NOTE: You MUST Replace : $FORM_FIELD_FILE_ID = ‘image_upload‘; 

with the name of YOUR Form.

 

If you don’t the Form will result in an Error like: x There was an error while trying to upload your file.