Skip to content

Instantly share code, notes, and snippets.

@mathiasmadsen
Created March 23, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathiasmadsen/afd06d5380f74c080fb104ac5fd6eaea to your computer and use it in GitHub Desktop.
Save mathiasmadsen/afd06d5380f74c080fb104ac5fd6eaea to your computer and use it in GitHub Desktop.
// Send copy, if radio checked – Based on http://www.motomax.de/wordpress/contact-form-7-send-copy-to-sender-with-checkbox-in-3-9-update/
function check_mail_send_contactform($cf7) {
//get CF7's mail and posted_data objects
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$mail = $cf7->prop( 'mail' );
if(isset($posted_data['checkbox'][0])) { //if Checkbox checked
$mail2 = $cf7->prop( 'mail_2' ); //get CF7's mail_2 object
//now set sender's address to mail2's recipient
$mail2['recipient'] = $posted_data['your-email'];
$cf7->set_properties( array( 'mail_2' => $mail2 ) );
}
return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');
@mathiasmadsen
Copy link
Author

FYI: This still works with latest Contact Form 7 implementation (5.3) and the latest Wordpress installation (5.5.3).

Note that 'checkbox' reflects the name in the contact form (the second string), e.g.
[checkbox checkbox use_label_element "Send a copy of my email e-mail"]

I was just lazy and decided to call the checkbox field 'checkbox' whereas I can see jose-serrao used the name 'contact_sendmail'.

@johnzuh
Copy link

johnzuh commented Jan 11, 2021

This did it for me too if(isset($posted_data['contact_sendmail'][0]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment