Description
Action that fires during form entry processing, after initial field validation has passed.
Parameters
- $fields
- array Sanitized entry field values/properties.
- $entry
- array Original $_POST global.
- $form_data
- array Form settings/data.
Source
wpforms/includes/class-process.php
More Information
The wpforms_process
action fires towards the end of form entry processing. It runs after all initial form fields have been validated and sanitized.
This hook should be used when the action potentially needs to return an error and halt form processing (see Examples below for details).
Examples: verify if a user account exists or validate data for a payment gateway.
Please remember that the form processing can still be halted/prevented after this hooks fires, by other code using the same hook.
See the note below about another similar hook that fires after all validation is passed.
Important note: If you are doing processing and do not need validation or need the entry ID, see wpforms_process_complete.
Examples
In the example code, you’ll see we’re first checking the form ID to make sure it matches the form that’s being targeted. Then we’re checking a specific field (by the field ID) to see if it’s empty.
Just remember to change the form ID from 5
to match your form ID and change the '4'
to match your field ID.
/** * Action that fires during form entry processing after initial field validation. * * @link https://wpforms.com/developers/wpforms_process/ * * @param array $fields Sanitized entry field. values/properties. * @param array $entry Original $_POST global. * @param array $form_data Form data and settings. */ function wpf_dev_process( $fields, $entry, $form_data ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #5. if ( absint( $form_data[ 'id' ] ) !== 5 ) { return $fields; } // check the field ID 4 to see if it's empty and if it is, run the error if(empty( $fields[4][ 'value' ]) ) { // Add to global errors. This will stop form entry from being saved to the database. // Uncomment the line below if you need to display the error above the form. // wpforms()->process->errors[ $form_data[ 'id' ] ][ 'header' ] = esc_html__( 'Some error occurred.', 'plugin-domain' ); // Check the field ID 4 and show error message at the top of form and under the specific field wpforms()->process->errors[ $form_data[ 'id' ] ] [ '4' ] = esc_html__( 'Some error occurred.', 'plugin-domain' ); // Add additional logic (what to do if error is not displayed) } } add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );
FAQ
Q: How would I target this for two different forms?
A: If you want to use this function for only some forms but not all, you just need to use the symbol in PHP that means “or“.
For example, in the snippet above, you can see if ( absint( $form_data[ 'id' ] ) !== 5 )
, this simply means that we’re only going to run that snippet on the form ID 5. But what if we had a few different forms we wanted this to run on?
There are a couple of options, let’s look at each option below.
1) Using multiple functions
If you wanted to just duplicate the function, give the function a new name and update the form ID number. You would copy and paste the entire function and then change this name wpf_dev_process
to something unique at the beginning and end of the function. This is not the cleanest way as it will add many extra lines to your functions file but it is easier to read and still effective.
2) Adding in the additional form IDs
The easiest way is to just use the PHP statement or
and add in the additional ID numbers. The PHP symbol for or is ||and form ID 7, you would if ( absint( $form_data[ 'id' ] ) !== 5 || $form_data[ 'id' ] ) !== 7)
.
Reference Articles
- How to Validate a Social Media URL Field in Your Form
- How to Restrict Numbers in a Single Line Text Form Field
- How to Provide an Age Restriction on the Datepicker Form Field
- How to Compare Two Dates in the Same Form
- How to Add Zip Code Field Validation on Your Forms
- How to Perform Field Comparisons Within Your WPForms
- How to Add Address Field Validation For Authorize.net