Generate sequential order numbers for WPForms submissions.
Add a hidden field to your form named “Order Number” and add a CSS class of order-number
. When the form is submitted, the following code will update that field to have a sequential order number.
It stores the order number as a site-option and increments the number on successful form submission. It creates a separate option for each form so you can have multiple order forms with their own sequential numbering.
If you want a single sequential sequence used for all forms, set $use_single_sequence = true;
.
I’m formatting the order number to have eight digits (ex: 00000001
). You can change the number of digits by changing the “8” in %08d
at the end.
/**
* WPForms, Sequential Order Number
* @author Bill Erickson
* @link https://www.billerickson.net/code/wpforms-sequential-order-number
*/
function be_wpforms_order_number( $fields, $entry, $form_data ) {
$use_single_sequence = false;
$order_field_id = false;
foreach( $form_data['fields'] as $field ) {
if( empty( $field['css'] ) )
continue;
$classes = explode( ' ', $field['css'] );
if( in_array( 'order-number', $classes ) )
$order_field_id = $field['id'];
}
if( empty( $order_field_id ) )
return $fields;
$option = $use_single_sequence ? 'be_wpforms_order' : 'be_wpforms_' . $form_data['id'] . '_order';
$order = intval( get_option( $option ) );
$order++;
update_option( $option, $order );
$fields[ $order_field_id ]['value'] = sprintf( '%08d', $order );
return $fields;
}
add_filter( 'wpforms_process_filter', 'be_wpforms_order_number', 10, 3 );