Fluent Forms includes basic conditional logic for routing form submissions to different email addresses in their UI, but what happens when you need more complex routing logic? Maybe you need to send to multiple recipients based on specific conditions, or route everything else to a catch-all address. This snippet gives you complete control over your form’s email routing using PHP.
How It Works
The fluentform/email_to filter allows you to programmatically change where form notifications are sent based on any data in the submission. This is perfect for scenarios where the built-in conditional logic falls short, like sending to multiple addresses for certain selections while routing everything else to a default recipient.
The filter passes you four parameters:
$address– The original email address(es)$notification– The notification settings object$submittedData– An array of all submitted form data$form– The form object (includes the form ID)
On to the Code!
Here’s a generic version you can adapt for your forms:
<?php
/**
* Advanced conditional email routing for Fluent Forms
* Customize the form ID, field name, and routing logic below
*/
add_filter('fluentform/email_to', function($address, $notification, $submittedData, $form) {
// Only apply to your specific form
if ($form->id != 1) { // Change to your form ID
return $address;
}
// Check if the field exists in the submission
if (empty($submittedData['your_field_name'])) { // Change to your field name
return $address;
}
$fieldValue = $submittedData['your_field_name'];
// Add your conditional routing logic
if ($fieldValue === 'Option 1') {
return '[email protected]';
}
if ($fieldValue === 'Option 2') {
// Send to multiple recipients
return '[email protected],[email protected]';
}
// Catch-all for any other values
return '[email protected]';
}, 10, 4);Key customizations:
- Change the form ID on line 8
- Update the field name on line 13 to match your form field
- Modify the conditional logic starting at line 18 to match your needs
- Use comma-separated addresses for multiple recipients
Example Use Case
Let’s say you run a real estate company with a contact form that includes a “Property Type” dropdown. You want:
- Residential inquiries to go to your residential team
- Commercial inquiries to go to both your commercial agent AND the office manager
- Land inquiries to go to your investment specialist
- Everything else (like “Not Sure” or “Other”) to go to your general inbox
Here’s how that would look:
<?php
add_filter('fluentform/email_to', function($address, $notification, $submittedData, $form) {
if ($form->id != 5) {
return $address;
}
if (empty($submittedData['property_type'])) {
return $address;
}
$propertyType = $submittedData['property_type'];
if ($propertyType === 'Residential') {
return '[email protected]';
}
if ($propertyType === 'Commercial') {
return '[email protected],[email protected]';
}
if ($propertyType === 'Land') {
return '[email protected]';
}
// Catch-all for "Not Sure" or any other options
return '[email protected]';
}, 10, 4);This approach ensures no inquiry falls through the cracks, and you have complete flexibility over your routing logic without being limited by the form builder’s UI.
Found this Snippet useful?
Consider buying me a coffee! ☕️ Your support helps me create more free snippets for the WordPress community.