Retrieving data from a form created with Contact Form 7

If you want to get the data from a form created with Contact Form 7 you can use the ‘wpcf7_before_send_mail’ hook. In your functions.php or from your plugin add action like follows:

add_action( 'wpcf7_before_send_mail', 'my_plugin_wpcf7_before_send_mail' );

function my_plugin_wpcf7_before_send_mail ( $contact_form ) {
    // TODO: get the data

Since version 3.9 Contact Form 7 removed $contact_form->posted data so this hook might seem like it is no longer working.

However we can still get the data but using a bit different approach provided by the Contact Form 7 API

function my_plugin_wpcf7_before_send_mail ( $contact_form ) {
    $wpcf7_submission = WPCF7_Submission::get_instance();

    $posted_data = $wpcf7_submission->get_posted_data();

    // in $posted_data you have the things sent via the form
    // try $posted_data[ 'your-email' ] to get the email from a default contact form

Using the $posted_data retrieved in the shown way we can get any of the fields that were sent by the user using the created form.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.