So you've updated a form and need to test it, what do you do? Do you fill a test submission? What about a second test? A third One? Do you risk looking unprofessional and/or filling users mailbox with tests?
Here's a quick solution for cf7 forms:
add_filter('wpcf7_before_send_mail', 'check_testing_fields', 10, 3); function check_testing_fields($contact_form, &$abort, $submission) { $search = "testing";//string to find $email_redirect = "example@gmail.com" // Get all form fields $posted_data = $submission->get_posted_data(); // Flag to track if we found "testing" in any field $contains_testing = false; // Check each field for the word "testing" foreach ($posted_data as $field_value) { // Handle arrays (like checkboxes or multi-select) if (is_array($field_value)) { foreach ($field_value as $value) { if (stripos($value, $search) !== false) { $contains_testing = true; break 2; } } } else { if (stripos($field_value, $search) !== false) { $contains_testing = true; break; } } } // If "testing" was found, modify the mail properties if ($contains_testing) { $mail = $contact_form->prop('mail'); $mail['recipient'] = $email_redirect; $contact_form->set_properties(array('mail' => $mail)); } return $contact_form; }
What Does it do?
Hook into wpcf7_before_send_mail to edit the data before email is sent, search for key word (testing), replace the recipient if found.
Just paste this code into your functions.php file and enjoy looking professional!
Leave a Reply