The code snippet below demonstrates how to handle file uploads in WP Forms using WordPress functions and hooks. This code can be useful when you want to customize the file upload functionality in WP Forms, such as validating file types, renaming files, or saving them to a specific location.
/**
* Handle file uploads in WP Forms.
*/
function wpsnippets_handle_wpforms_file_uploads( $fields, $entry, $form_data ) {
// Get the uploaded file field IDs
$file_field_ids = array( 1, 2, 3 ); // Replace with your file field IDs
foreach ( $file_field_ids as $field_id ) {
// Check if the file field has a value
if ( isset( $fields[ $field_id ]['value'] ) && ! empty( $fields[ $field_id ]['value'] ) ) {
$file = $fields[ $field_id ]['value'];
// Perform your custom file handling logic here
// For example, you can validate file types, rename files, or save them to a specific location
// Example: Save the file to a custom directory
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . basename( $file['name'] );
move_uploaded_file( $file['tmp_name'], $file_path );
// Update the field value with the new file path
$fields[ $field_id ]['value'] = $file_path;
}
}
return $fields;
}
add_filter( 'wpforms_process_entry_fields', 'wpsnippets_handle_wpforms_file_uploads', 10, 3 );
In this code snippet, we define a custom function wpsnippets_handle_wpforms_file_uploads
that hooks into the wpforms_process_entry_fields
filter. This filter allows us to modify the form fields before they are processed and saved.
Inside the function, we first specify the file field IDs that we want to handle. You should replace the array of IDs ($file_field_ids
) with the actual file field IDs from your WP Forms.
Next, we iterate through each file field and check if it has a value. If a file is uploaded, we can perform our custom file handling logic. In the example code, we save the file to the default WordPress uploads directory using the wp_upload_dir()
function and move the uploaded file using move_uploaded_file()
.
Finally, we update the field value with the new file path so that it gets saved correctly in the form entry.
Remember to customize the file handling logic based on your specific requirements.
Examples
Example 1: Allowing File Uploads in a WP Forms Contact Form
This use case demonstrates how to enable file uploads in a WP Forms contact form. By adding a file upload field to the form and configuring the necessary settings, users will be able to submit files along with their form submissions.
add_filter( 'wpforms_validate_field', 'wpsnippets_enable_file_upload', 10, 2 );
function wpsnippets_enable_file_upload( $field, $form_data ) {
if ( $field['type'] === 'fileupload' ) {
$field['file_extensions'] = 'pdf, doc, docx'; // Specify allowed file extensions
$field['max_file_size'] = 5; // Set maximum file size in MB
}
return $field;
}
This code snippet uses the wpforms_validate_field
filter to modify the file upload field settings. By checking if the field type is fileupload
, we can customize the allowed file extensions and maximum file size. In this example, we allow PDF, DOC, and DOCX files with a maximum size of 5 MB.
Example 2: Limiting File Uploads to a Specific Number
In this use case, we’ll explore how to restrict the number of file uploads a user can submit through a WP Forms file upload field. By setting a maximum file count, you can control the number of files users can upload.
add_filter( 'wpforms_validate_field', 'wpsnippets_limit_file_uploads', 10, 2 );
function wpsnippets_limit_file_uploads( $field, $form_data ) {
if ( $field['type'] === 'fileupload' ) {
$field['max_files'] = 3; // Set maximum file count
}
return $field;
}
Using the wpforms_validate_field
filter, we can modify the file upload field settings to include a maximum file count. In this example, we limit the number of file uploads to 3.
Example 3: Customizing File Upload Error Messages
This use case demonstrates how to customize the error messages displayed when users encounter issues with file uploads in a WP Forms form. By modifying the error messages, you can provide more specific instructions or explanations to users.
add_filter( 'wpforms_frontend_errors', 'wpsnippets_custom_file_upload_error_message', 10, 2 );
function wpsnippets_custom_file_upload_error_message( $errors, $form_data ) {
foreach ( $errors as $key => $error ) {
if ( $error === 'File type not allowed.' ) {
$errors[ $key ] = 'Only PDF, DOC, and DOCX files are allowed.';
} elseif ( $error === 'File size too large.' ) {
$errors[ $key ] = 'Please ensure the file size is within 5 MB.';
}
}
return $errors;
}
By hooking into the wpforms_frontend_errors
filter, we can modify the error messages related to file uploads. In this example, we check for specific error messages like “File type not allowed” and “File size too large” and replace them with more descriptive messages.