Routing Contact Form 7 Emails Based on a Select Field
Contact Form 7 is one of the most popular WordPress plugins for creating forms, it is open-source and totally free. Recently, I implemented a feature to route form emails to different recipients based on the user’s selection in a dropdown field. This can be particularly useful for websites with multiple departments or teams handling specific inquiries.
In this article, I’ll walk you through the script I used to achieve this.
The Use Case
Imagine a contact form with a dropdown field named department
. Each option corresponds to a department, and we want the form to send an email to the appropriate email address based on the user’s selection. Here’s how to do it.
The Contact Form
Here’s an example of the Contact Form 7 markup for the form:
<label> Select
[select* department "Selection1" "Selection2" "Selection3"] </label>
<label> Name
[text* your-name autocomplete:name] </label>
<label> Email
[email* your-email autocomplete:email] </label>
<label> Subject
[text* your-subject] </label>
<label> Message
[textarea your-message] </label>
[submit "Send"]
The Code
Below is the script I added to my theme’s functions.php
file:
add_action('wpcf7_before_send_mail', 'dbm_cf7_recipient_routing', 10, 3);
dbm_cf7_recipient_routing($contact_form, &$abort, $submission) {
$email_routing = [
'Selection1' => '[email protected]',
'Selection2' => '[email protected]',
'Selection3' => '[email protected]',
];
if ( $submission ) {
// Retrieve the selected option from the 'topic' field
$selected_department = $submission->get_posted_data('department');
// Convert to a string in case it's an array (This is important)
$selected_department = implode( ', ', (array) $selected_department );
// Update the recipient email in the form properties
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $email_routing[$selected_department];
$contact_form->set_properties($properties);
return $contact_form;
}
}
Breaking Down The Code
- Hook into
wpcf7_before_send_mail
: This hook runs just before Contact Form 7 sends the email. It’s ideal for modifying the email properties. - Define the
$email_routing
array: This associative array maps dropdown options (e.g.,Selection1
) to their corresponding email addresses. - Retrieve the selected option: Using
$submission->get_posted_data('department')
, we fetch the user’s selection from thetopic
field. - Handle array values: Contact Form 7 may return field data as an array, so we use
implode()
to ensure we’re working with a string. - Update the email recipient: We modify the form’s email properties to replace the default recipient with the address corresponding to the selected department.
- Set the updated properties: Finally, we use
set_properties()
to apply the changes to the form.
Code Variations
Route Emails for Specific Contact Form
Each contact form has an unique ID.
You can find the ID from the form Shortcode.
[contact-form-7 id="acd56df" title="Contact Form 1"
]
add_action('wpcf7_before_send_mail', 'dbm_cf7_recipient_routing_by_form_id', 10, 3);
dbm_cf7_recipient_routing($contact_form, &$abort, $submission) {
$form_id = $contact_form->id();
if($form_id == acd56df ){
// Your Code
}
}