We use the Avada theme's form builder for one form on the site (I think the builder plugin actually does the functionality). This form can be send to email and stored to database. The email contains the form data and is send to the site owner.
In the theme/plugin, we are unable to also send a thank you note to the person that submitted the form. There is an option to send the form to another page (php file) instead of saving to database adn sending the contents to site owner.
This last option will use the admin-ajax from WP to submit the data to that php file. I coded the 'thank you' email there (more as a test for myself), this works. When I submit the form, I get a thank you email. If possible, we would like to use the excisting option of saving and sending the form data.
I did some searching in the plugin files and found the class 'Fusion_Form_Submit'. This contains the public function: ajax_submit_form_to_database_email(), that seems to handle the form submit.
public function ajax_submit_form_to_database_email() {
// Checks nonce, recaptcha and similar. Dies if checks fail.
$this->pre_process_form_submit();
$data = $this->get_submit_data();
$sendmail = $this->submit_form_to_email( $data );
// We don't want internal email fields to be saved in db.
$data = $this->remove_internal_email_fields( $data );
$this->submit_form_to_database( $data );
if ( $data['submission']['form_id'] && true === $sendmail ) {
$fusion_forms = new Fusion_Form_DB_Forms();
$fusion_forms->increment_submissions_count( $data['submission']['form_id'] );
die( wp_json_encode( $this->get_results_from_message( 'success', 'db_email_saved' ) ) );
}
die( wp_json_encode( $this->get_results_from_message( 'error', 'db_email_failed' ) ) );
}
I have not really worked with Classes. I can read kind of what it does and I assume you can use this following part, where you would need the 'database_email' actions I think.
public function __construct() {
foreach ( [ 'database', 'email', 'url', 'database_email' ] as $method ) {
add_action( "wp_ajax_fusion_form_submit_form_to_$method", [ $this, "ajax_submit_form_to_$method" ] );
add_action( "wp_ajax_nopriv_fusion_form_submit_form_to_$method", [ $this, "ajax_submit_form_to_$method" ] );
}
$this->init_recaptcha();
}
So I have the custom php script that receives the ajax post data from the form - I put this in my own custom plugin for now.
define( 'WP_USE_THEMES', false );
require_once( '../../../wp-load.php' );
if ( $_POST ) {
$to = '***@****';
$subject = 'test';
// Build the body based on your form...
$email = sanitize_email($_POST['email']);
$body = sanitize_text_field($_POST['plaats']) ;
$message = "testbericht: <br>";
$message.= "email: $email <br>";
$message.= "test: <br>";
$message.= $body;
$headers = array('Content-Type: text/html; charset=UTF-8');
$headers[] = 'From: Website **** <***@***>';
wp_mail( $to, $subject, $message, $headers );
}
How can I use the above class and functions on on this same script that send the 'thank you' note?