x

VS

An Independent Development Company

 

Contact Form 7 Dynamic Recipient Based on Select Drop Down

We recently had a project where we wanted to use Contact Form 7 where the user’s message would send to different email addresses based on a drop-down selection menu. The solution involved using the “wpcf7_before_send_mail” filter and then accessing the data to run a conditional. After some trial and error, the final code is below, let us know your thoughts in the comment section below:

add_filter( 'wpcf7_before_send_mail', 'davevsdave_dynamic_email_based_on_dropdown' );
function davevsdave_dynamic_email_based_on_dropdown( $contact_form ) {
//Get the form ID
$form_id = $contact_form->id();
//If it's our form with the select dropdown, let's do something...
if( $form_id == 9487 ) {
//Get the dorpdown value - change "regarding" to the name of your select dropdown menu
$regarding = isset( $_POST['regarding'] ) ? trim( $_POST['regarding'] ) : '';
//Get the contact form properties so we can change where the email is being sent
$properties = $contact_form->get_properties();
//Run your conditional and update the email property here. You'll want to update the conditionals to your select drop down values. If you have more than three, consider using a Switch/Case statement instead.
if(!empty($regarding)):
if($regarding === 'Careers'):
$properties['mail']['recipient'] = 'careers@example.com';
elseif($regarding === 'New Business'):
$properties['mail']['recipient'] = 'newpartnerships@example.com';
else:
$properties['mail']['recipient'] = 'frontdesk@example.com';
endif;
//Update the contact object properties with the new email address
$contact_form->set_properties($properties);
endif;
}
//Finally, return the form, and that's it!
return $contact_form;
}

Comments:


Exactly what I needed! Thanks for sharing.

Posted by Chris on January 23, 2022 Reply

Join The Discussion:

Your email address will not be published. Required fields are marked *


We’re here to help


Are you tired of feeling lost in the vast world of WordPress development? Our team of professionals is here to guide you every step of the way. With expertise in UI design, UX strategy, and development, we’ll craft a custom solution that meets your specific needs and helps your website stand out from the competition. So why wait? Take the first step towards a better website today and contact us to learn more.

Contact us

Ready to Revamp Your CMS Website? Let’s Go!

Don’t hesitate, let’s collaborate! We can’t wait to hear from you and see what we can do.






    We Value Your Privacy. We’ll never sell or share your data.