Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davevsdave/a21be669e4c36794b69f4f629cb2fd41 to your computer and use it in GitHub Desktop.

Select an option

Save davevsdave/a21be669e4c36794b69f4f629cb2fd41 to your computer and use it in GitHub Desktop.
Wordpress | Contact Form 7 | Dynamic Email Based on Dropdown Selection
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment