add_action('wpcf7_before_send_mail', function($contact_form) { $submission = WPCF7_Submission::get_instance(); if ($submission) { $uploaded_files = $submission->uploaded_files(); if (isset($_FILES['selected-files']) && !empty($_FILES['selected-files']['tmp_name'][0])) { $files = array(); foreach ($_FILES['selected-files']['tmp_name'] as $key => $tmp) { if (!empty($tmp) && file_exists($tmp)) { $upload_dir = wp_upload_dir(); $cf7_upload_dir = $upload_dir['basedir'] . '/wpcf7_uploads'; wp_mkdir_p($cf7_upload_dir); $filename = $_FILES['selected-files']['name'][$key]; $new_path = $cf7_upload_dir . '/' . wp_unique_filename($cf7_upload_dir, $filename); if (copy($tmp, $new_path)) { $files[] = $new_path; wp_schedule_single_event(time() + 300, 'delete_cf7_temp_file', array($new_path)); } } } if (!empty($files)) { $uploaded_files['selected-files'] = $files; $reflection = new ReflectionClass($submission); $property = $reflection->getProperty('uploaded_files'); $property->setAccessible(true); $property->setValue($submission, $uploaded_files); error_log('Forced selected-files into CF7: ' . print_r($files, true)); } } } }, 1); add_action('delete_cf7_temp_file', function($file) { if (file_exists($file)) { @unlink($file); } }); add_filter('wp_mail', function($args) { if (empty($args['attachments']) && isset($_FILES['selected-files'])) { $args['attachments'] = array(); foreach ($_FILES['selected-files']['tmp_name'] as $key => $tmp) { if (!empty($tmp) && file_exists($tmp)) { $upload_dir = wp_upload_dir(); $temp_dir = $upload_dir['basedir'] . '/mail_temp'; wp_mkdir_p($temp_dir); $filename = $_FILES['selected-files']['name'][$key]; $new_path = $temp_dir . '/' . $filename; if (copy($tmp, $new_path)) { $args['attachments'][] = $new_path; wp_schedule_single_event(time() + 300, 'delete_mail_temp_file', array($new_path)); } } } error_log('Forced attachments in wp_mail: ' . print_r($args['attachments'], true)); } return $args; }, 1); add_action('delete_mail_temp_file', function($file) { if (file_exists($file)) { @unlink($file); } });