Last updated on October 18, 2023

WP Forms hooks and filters

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Use hooks and filters to extend WP Forms.

WP Forms hooks and filters are useful for customizing and extending the functionality of WP Forms, a popular WordPress plugin for creating forms. By using hooks and filters, you can modify the behavior of WP Forms without directly modifying its core files. This allows for easier updates and maintenance of your WordPress site.

One common use case is to add custom validation to a WP Forms form. You can use the wpforms_validate_field filter to add your own validation logic to a specific form field. Here’s an example:

function wpsnippets_custom_validation( $errors, $field, $form_data ) {
    // Perform your custom validation logic here
    if ( $field['id'] === 'my_field' && empty( $field['value'] ) ) {
        $errors[] = 'Please enter a value for the field.';
    }

    return $errors;
}
add_filter( 'wpforms_validate_field', 'wpsnippets_custom_validation', 10, 3 );

In this example, the wpsnippets_custom_validation function is hooked to the wpforms_validate_field filter. It receives three parameters: $errors (an array of validation errors), $field (the current form field being validated), and $form_data (an array of form data). The function checks if the field ID is ‘my_field’ and if it’s empty, it adds a validation error to the $errors array.

Another use case is to modify the form submission data before it is processed. You can use the wpforms_process_entry_data filter to manipulate the form data. Here’s an example:

function wpsnippets_modify_form_data( $entry_data, $form_data ) {
    // Modify the form submission data here
    $entry_data['fields']['my_field'] = 'Modified value';

    return $entry_data;
}
add_filter( 'wpforms_process_entry_data', 'wpsnippets_modify_form_data', 10, 2 );

In this example, the wpsnippets_modify_form_data function is hooked to the wpforms_process_entry_data filter. It receives two parameters: $entry_data (an array of form submission data) and $form_data (an array of form data). The function modifies the value of the ‘my_field’ field in the $entry_data array.

These are just a few examples of how you can use hooks and filters to customize WP Forms. By leveraging the available hooks and filters, you can extend the functionality of WP Forms to suit your specific needs.

Examples

Example 1: Customize form fields in WP Forms

This example demonstrates how to customize form fields in WP Forms using hooks and filters. The code example below shows how to add a custom class to a specific form field.

function wpsnippets_customize_form_fields( $fields ) {
    $fields['field_id']['class'][] = 'custom-class';
    return $fields;
}
add_filter( 'wpforms_field_properties', 'wpsnippets_customize_form_fields' );

In this code example, we define a custom function wpsnippets_customize_form_fields that takes an array of form fields as a parameter. We then add the 'custom-class' to the class property of the specific form field with the ID 'field_id'. Finally, we return the modified array of form fields. The wpforms_field_properties filter is used to apply this customization.

Example 2: Modify form submission data in WP Forms

This example demonstrates how to modify form submission data in WP Forms using hooks and filters. The code example below shows how to add a custom field to the form submission data.

function wpsnippets_modify_form_submission_data( $fields, $entry ) {
    $fields['custom_field'] = 'Custom Value';
    return $fields;
}
add_filter( 'wpforms_process_entry_fields', 'wpsnippets_modify_form_submission_data', 10, 2 );

In this code example, we define a custom function wpsnippets_modify_form_submission_data that takes two parameters: $fields (an array of form submission data) and $entry (an array of form entry data). We then add a custom field 'custom_field' with the value 'Custom Value' to the $fields array. Finally, we return the modified $fields array. The wpforms_process_entry_fields filter is used to apply this modification.

Example 3: Customize form notification emails in WP Forms

This example demonstrates how to customize form notification emails in WP Forms using hooks and filters. The code example below shows how to modify the email subject and content.

function wpsnippets_customize_form_notification_email( $fields, $form_data, $entry ) {
    $fields['subject'] = 'Custom Subject';
    $fields['message'] = 'Custom Content';
    return $fields;
}
add_filter( 'wpforms_email_message', 'wpsnippets_customize_form_notification_email', 10, 3 );

In this code example, we define a custom function wpsnippets_customize_form_notification_email that takes three parameters: $fields (an array of email fields), $form_data (an array of form data), and $entry (an array of form entry data). We then modify the 'subject' and 'message' fields of the $fields array to have custom values. Finally, we return the modified $fields array. The wpforms_email_message filter is used to apply this customization.

Last updated on October 18, 2023. Originally posted on December 17, 2023.

Leave a Reply

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