How to get form data in Contact Form 7

How to get form data in Contact Form 7

Contact form 7 is a wordpress plugin used to add form in a wordpress website without adding any custom code. Contact form 7 allow us an area in admin panel where we can create as many forms that we want and we can add several fields on a simple click one by one. We can also manage email message, which notify admin or user. We can add multiple email addresses as recover. We can also include file upload option with contact form 7. Most of the websites which are built in wordpress prefer contact form 7.

First we have to check the form is working fine and sent an email to defined email in admin panel. We will use `wpcf7_before_send_mail` hook which is perform automatically when a contact form 7 form submitted successfully. `wpcf7_before_send_mail` will fire before email will sent to the admin. Use below code in functions.php to get form data.

add_action( 'wpcf7_mail_sent', 'custom_wpcf7_mail_sent_function' ); 

function custom_wpcf7_mail_sent_function( $contact_form ) {
    $title = $contact_form->title;  //get contact form name 
    $submission = WPCF7_Submission::get_instance();  
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }
}

Also Read: How to create foreign key constraints in Laravel
Also Read: Capture page screenshot by URL PHP script

Contact form 7 is a wordpress plugin used to add form in a wordpress website without adding any custom code. Contact form 7 allow us an area in admin panel where we can create as many forms that we want and we can add several fields on a simple click one by one. We can also manage email message, which notify admin or user. We can add multiple email addresses as recover. We can also include file upload option with contact form 7. Most of the websites which are built in wordpress prefer contact form 7.

First we have to check the form is working fine and sent an email to defined email in admin panel. We will use `wpcf7_before_send_mail` hook which is perform automatically when a contact form 7 form submitted successfully. `wpcf7_before_send_mail` will fire before email will sent to the admin. Use below code in functions.php to get form data.

add_action( 'wpcf7_mail_sent', 'custom_wpcf7_mail_sent_function' ); 

function custom_wpcf7_mail_sent_function( $contact_form ) {
    $title = $contact_form->title;  //get contact form name 
    $submission = WPCF7_Submission::get_instance();  
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }
}

Also Read: How to create foreign key constraints in Laravel
Also Read: Capture page screenshot by URL PHP script

How to install contact form 7

  • Login wordpress admin panel and click on Add New under Plugins.
  • You will see a input search field, enter `Contact Form 7` and hit enter &
  • After that click on Install now button. When plugin will installed successfully, click on `Activate` to activate the plugin.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Russel
Russel
21 Dec 2020

It works. Thanks

Rinku Thakur
Rinku Thakur
22 Dec 2020

Cheers

Carlos vergus
Carlos vergus
27 Dec 2020

I've created contact form 7 addon plugin using this article. 🙂

Gary
Gary
31 Dec 2020

Removed an addon and used it custom and it workss

Barber https://netflix.com/
Barber https://netflix.com/
15 Oct 2021

Barber

Add your comment

Close