The wpcf7_mail_sent is a contact form 7 action hook. You can see more details on docs.wpdebuglog.com
How do I get submission data from the wpcf7_mail_sent hook?
Add the following code in your theme functions.php or plugin file to get post data.
We can access data from the WPCF7_Submission class instance and get_posted_data method.
1 2 3 4 5 6 7 8 9 10 11 |
<?php add_action('wpcf7_mail_sent', 'my_custom_mail_sent' ); function my_custom_mail_sent( $contact_form ){ // to get form id $form_id = $contact_form->id(); // to get submission data // $submission = WPCF7_Submission::get_instance(); //$posted_data = $submission->get_posted_data(); } |
The wpcf7_mail_sent is not working
The wpcf7_mail_sent function will not work if the email function has any issues. We can solve the SMTP issue with the SMTP plugin. We can use the wpcf7_before_send_mail action hook if you want to hook before the mail function execution.
The wpcf7_before_send_mail Hook Not Working
First, deactivate contact form 7 related plugins from wp-admin > plugins. Then check the Chrome console log error.
Check $abort
the variable is true
in wpcf7_before_send_mail
hook.
The wpcf7_before_send_mail Documentation
The wpcf7_before_send_mail hook triggers the before-send mail function. It can be used to save entries to the database.
Save submitted data to MySQL database columns by the CF7ADB database plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function my_action_wpcf7_before_send_mail( $contact_form, $abort ) { // to get form id $form_id = $contact_form->id(); // to get submission data $posted_data is asociative array $submission = WPCF7_Submission::get_instance(); $posted_data = $submission->get_posted_data(); // It will abort mail function if we assing $abort = true; } // add the action add_action( 'wpcf7_before_send_mail', 'my_action_wpcf7_before_send_mail', 10, 2 ); |
The wpcf7_before_send_mail gets form id $form_id = $contact_form->id();
We can abort contact form 7 by assigning $abort = true;
The wpcf7_before_send_mail gets the post ID
Use global $post;
The variable is the current custom post type or page post. Get post id by $post->ID;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function my_action_wpcf7_before_send_mail( $contact_form ) { // to get form id $form_id = $contact_form->id(); //to get current page id global $post; $post_id = $post->ID; } // add the action add_action( 'wpcf7_before_send_mail', 'my_action_wpcf7_before_send_mail', 10, 1 ); // to get form id $form_id = $contact_form->id(); } // add the action add_action( 'wpcf7_before_send_mail', 'my_action_wpcf7_before_send_mail', 10, 1 ); |
The wpcf7_before_send_mail For Specific Form
Check the id of the form by if
condition in a callback function.
Example : [contact-form-7 id="39" title="Contact form 1"]
form id is 39.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function my_action_wpcf7_before_send_mail( $contact_form ) { // to get form id $form_id = $contact_form->id(); if($form_id == 39 ){ // to get submission data $posted_data is asociative array $submission = WPCF7_Submission::get_instance(); $posted_data = $submission->get_posted_data(); } } // add the action add_action( 'wpcf7_before_send_mail', 'my_action_wpcf7_before_send_mail', 10, 1 ); |
The wpcf7_before_send_mail Get Form Data
We can access post data from $_POST
variable.
Example:-
1 2 3 4 5 6 7 8 9 |
function my_action_wpcf7_before_send_mail( $contact_form ) { // to get form id $form_id = $contact_form->id(); var_dump($_POST); } // add the action add_action( 'wpcf7_before_send_mail', 'my_action_wpcf7_before_send_mail', 10, 1 ); |
Check chrome > network > check post request response.
How do you save data to the database?
We can use this action hook and save by using $wpdb or MySQL functions or we can use the CFDB7 plugin or CF7ADB plugin. CF7ADB is easy to manipulate data.
How to Display Contact Form 7 Data in WordPress
Install CFDB7 or CF7ADB and display CFDB7 plugin. Then add shortcode [cfdb7-data form-id='{id}’]