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();
    }
}

How to install contact form 7

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

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

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
20 Dec 2020

It works. Thanks

Rinku Thakur
Rinku Thakur
21 Dec 2020

Cheers

Carlos vergus
Carlos vergus
27 Dec 2020

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

Gary
Gary
30 Dec 2020

Removed an addon and used it custom and it workss

Add your comment