Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id

Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Targeting wpcf7_before_send_mail to a Single Contact Form

When working with multiple instances of Contact Form 7 on a single WordPress site, global hooks like wpcf7_before_send_mail become tricky to manage. The hook passes a $contact_form object to your callback function, which contains the form definition and configuration for that specific instance. However, simply attaching an action listener without filtering based on the form ID means your logic executes for every submission across all forms.

If you are injecting dynamic data collected from hidden input fields into the email body, sending those values to unrelated forms can expose sensitive information or clutter inboxes with irrelevant data. To restrict this behavior to a specific form, you need to inspect the $contact_form object within your callback and verify its ID before modifying the mail properties.

Inspecting the Contact Form Object

The $contact_form argument provided to wpcf7_before_send_mail is an instance of WPCF7_Form. This object exposes several methods to retrieve metadata about the form being processed. The most critical property for identification is the ID, accessible via the id() method.

You can retrieve the ID using the following syntax:

$form_id = $contact_form->id();

This returns a string representing the unique identifier of the form. With this value, you can implement a conditional check to ensure your custom logic only runs for the intended form.

Implementing an ID Filter

To modify the email body exclusively for one form, wrap your existing logic inside an if statement that compares $form_id against the target ID. Here is how you should refactor the provided code snippet:

add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );

function wpcf7_add_text_to_mail_body($contact_form) {
    // Retrieve the current form's ID
    $form_id = $contact_form->id();

    // Define the target form ID here
    $target_form_id = '12345'; // Replace with your actual Form ID

    // Exit early if this is not the specific form we care about
    if ( $form_id !== $target_form_id ) {
        return;
    }

    // Proceed only if we are on the correct form
    $values_list = isset( $_POST['valsitems'] ) ? $_POST['valsitems'] : [];
    
    if ( empty( $values_list ) ) {
        return;
    }

    $values_str = implode(", ", $values_list);

    // get mail property
    $mail = $contact_form->prop( 'mail' ); 

    // add content to email body
    $mail['body'] .= PHP_EOL . 'INDUSTRIES SELECTED:' . PHP_EOL;
    $mail['body'] .= $values_str;


    // set mail property with changed value(s)
    $contact_form->set_properties( array( 'mail' => $mail ) );
}

Important Implementation Details

There are a few critical details to consider when working with this hook and the Contact Form 7 API.

First, always validate the existence of $_POST variables before accessing them. Using isset() prevents PHP warnings if the variable is missing, which can happen if the form submission fails partially or if debugging tools strip specific inputs.

Second, ensure you are using set_properties correctly. The method modifies the properties of the $contact_form object passed by reference. Once you call set_properties, the changes persist in the mail array associated with that form instance before it is sent. If you forget to set the property back, your modifications to the $mail array are discarded.

Third, be mindful of the order of operations. The wpcf7_before_send_mail hook fires after the default subject and body have been populated by Contact Form 7 based on the form's definition. Appending to $mail['body'] ensures your custom text appears at the end of the email, preserving any standard placeholders or dynamic fields defined in the [label] tags.

Troubleshooting Common Pitfalls

A common mistake is assuming that checking $_POST keys is sufficient to identify a form. Since different forms often use similar hidden field names (e.g., _wpcf7_referer, _wpcf7_version), relying solely on input data can lead to false positives. The form ID is the only reliable source of truth for distinguishing between instances.

Another potential issue arises if you modify the $mail array but fail to call set_properties. In such cases, the email sent will be identical to the default output because the modified array is never applied back to the object state that the plugin uses for transmission.

Finally, remember that the form ID is defined in the Contact Form 7 settings under the "General" tab of the form editor. If you are unsure which ID corresponds to your specific form, you can inspect the global $wpcf7 object or use a debugging tool like Query Monitor to log the id() property during a submission event.

Our network