Add Incremental ID to Individual Forms
-
The IDs (form_id) being assigned to form entries are incremental and are not assigned individually to the entry of an individual form. So the unique ID are actually not unique to an individual form.
To resolve this issue I created an additional function that calculates the total entries in each form +1 then used it as a value with SmartTag. Once I added the function I created a hidden field and added the SmartTag. Now I am getting a Unique ID in a proper increment for individual forms. BUT the problem is that the when multiple users have opened the form together they all get the same ID assigned leading and even though they submit at a different time they all get the same ID. Below is the code,
function wpf_dev_register_smarttag_contact( $tags ) { // Key is the tag, item is the tag name. $tags['totalcount_contactform'] = 'Total Count - Contact Form'; return $tags; } add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag_contact' ); function wpf_dev_process_smarttag_contact( $content, $tag ) { // Only run if it is our desired tag. if ( 'totalcount_contactform' === $tag ) { return wpforms()->entry->get_entries( array( 'form_id' => 12136 ), true )+1; // Replace the tag with our link. $content = str_replace( '{totalcount_contactform}', $link, $content ); } return $content; } add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag_contact', 10, 2 );
To overcome this problem, I am trying to create a function that would trigger on submission but it’s not working. Below is the code,
function wpf_dev_register_smarttag2( $tags ) { // Key is the tag, item is the tag name. $tags['totalcount'] = 'Total Count'; return $tags; } add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag2' ); function wpf_dev_unique_id( $fields, $entry, $form_data, $entry_id ) { // Limit to Form ID = 14501 if( 14501 != $form_data['id'] ) return; if ( 'totalcount' === $tag ) { return wpforms()->entry->get_entries( array( 'form_id' => 14501 ), true )+1; // Replace the tag with our link. $content = str_replace( '{totalcount}', $link, $content ); } return $content; } add_action( 'wpforms_process_complete', 'wpf_dev_unique_id', 10, 4 );
What is it that I am doing wrong?
- The topic ‘Add Incremental ID to Individual Forms’ is closed to new replies.