Hi @visedfaq Here is a code that will help you to achieve this but this might not be the perfect solution and you might need to modify the code according to your needs.
Add a Custom Function to Handle Form Submission:
You can add this code to your theme’s functions.php
file or use a plugin that allow to insert code something like WPCode.
// Handle Form Submission
add_action('wpforms_process_complete', 'generate_text_file_on_submission', 10, 4);
function generate_text_file_on_submission($fields, $entry, $form_data, $entry_id) {
// Check if it's the specific form ID you want to target
if ($form_data['id'] == 8) { // Replace 8 with your form ID
// Extract data from the form fields
$first_name = $fields[0]['first']; // Replace 0 with your field ID
$last_name = $fields[0]['last']; // Replace 0 with your field ID
$email = $fields[1]['value']; // Replace 1 with your field ID
$message = $fields[2]['value']; // Replace 2 with your field ID
// Static data
$static_data = "This is some static data.\n";
// Combine data
$file_content = "Name: $first_name $last_name\n";
$file_content .= "Email: $email \n";
$file_content .= "Message: $message \n";
$file_content .= $static_data;
// Generate text file
$text_file_name = 'generated_file.txt';
$upload_dir = wp_upload_dir();
$text_file_path = $upload_dir['basedir'] . '/' . $text_file_name;
file_put_contents($text_file_path, $file_content);
// Create ZIP file
$zip = new ZipArchive();
$zip_file_name = 'generated_file.zip';
$zip_file_path = $upload_dir['basedir'] . '/' . $zip_file_name;
if ($zip->open($zip_file_path, ZipArchive::CREATE) === TRUE) {
$zip->addFile($text_file_path, $text_file_name);
$zip->close();
}
// Store the ZIP file path in a session variable
session_start();
$_SESSION['file_path'] = $zip_file_path;
// Clean up the text file after adding to ZIP
unlink($text_file_path);
// Redirect to a URL that triggers the download
$redirect_url = add_query_arg('download_file', '1', site_url());
wp_redirect($redirect_url);
exit;
}
}
Add another function to serve the file for download.
// Handle File Download
add_action('init', 'handle_file_download');
function handle_file_download() {
if (isset($_GET['download_file']) && $_GET['download_file'] == '1') {
session_start();
if (isset($_SESSION['file_path'])) {
$file_path = $_SESSION['file_path'];
if (file_exists($file_path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
unlink($file_path); // Delete the file after download
exit;
}
}
}
}
Important Considerations:
- Security: Sanitize and validate user inputs to prevent security vulnerabilities.
- Sessions: Ensure your server supports sessions and they are properly configured in
php.ini
.
- Unique Filenames: Consider using unique filenames to prevent overwriting files and manage file cleanup.
This code should serve as a solid starting point. Adjust the field IDs and static data as needed to match your form and requirements.