How To Remove Specific Fields From {all_fields} in Notifications

Would you like to exclude certain fields from your form email notifications when using the {all_fields} smart tag? This can be particularly useful when you have hidden fields or other information you don’t want to include in every notification.

In this tutorial, we’ll show you how to remove specific fields from your email notifications while still using the {all_fields} smart tag.

Adding the Code Snippet

The {all_fields} smart tag conveniently includes all form fields in your email notifications. However, to exclude specific fields from {all_fields}, we’ll need to use a custom code snippet.

Once you’ve set up and saved your form, it’s now time to add the snippet. If you need any help with adding snippets to your site, please check out this tutorial.

To Remove a Single Field From {all_fields}:

/**
 * Remove Specific Fields from Notifications
 *
 * @link https://wpforms.com/developers/how-to-remove-specific-fields-from-notifications
 */
add_action('wpforms_loaded', function() {
    add_filter('wpforms_entry_email_data', function ($fields, $entry, $form_data) {
        // Bail early if form ID is not equal to 1000
        if ((int)$form_data['id'] !== 1000) {
            return $fields;
        }

        // Unset field from notifications with field ID #3
        if (isset($fields[3])) {
            unset($fields[3]);
        }

        return $fields;
    }, 11, 3);
});

To Remove Multiple Fields From {all_fields}:

/**
 * Remove Specific Fields from Notifications
 *
 * @link https://wpforms.com/developers/how-to-remove-specific-fields-from-notifications
 */
add_action('wpforms_loaded', function() {
    add_filter('wpforms_entry_email_data', function ($fields, $entry, $form_data) {
        // Bail early if form ID is not equal to 1000
        if ((int)$form_data['id'] !== 1000) {
            return $fields;
        }

        // Unset fields from notifications with field IDs #3, #4, and #5
        $fields_to_unset = [3, 4, 5];
        foreach ($fields_to_unset as $field_id) {
            if (isset($fields[$field_id])) {
                unset($fields[$field_id]);
            }
        }

        return $fields;
    }, 11, 3);
});

Note: You’ll need to replace the form ID (1000 in the examples above) and field IDs with your actual IDs. To find your form and field IDs, please check out this tutorial.

And that’s it! You’ve now set up your form to remove specific fields from email notifications. Would you like to learn more about customizing form notifications? Check out our tutorial on including field descriptions in email notifications.

Reference Filter

wpforms_entry_email_data