Support » Plugin: Contact Form by WPForms - Drag & Drop Form Builder for WordPress » Add Incremental ID to Individual Forms

  • Resolved theparzival

    (@theparzival)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter theparzival

    (@theparzival)

    After reading documentation, I Found the solution. Below is the code I am using to generate unique id for each form.

    
    /**
     * WPForms, Generating Individual Unique ID for a Form
     *
     * @param array $fields Sanitized entry field values/properties.
     * @param array $entry Original $_POST global.
     * @param array $form_data Form settings/data
     * @return array $fields
     */
    function be_wpforms_update_total_field( $fields, $entry, $form_data ) {
    
    	// Only run on my form with ID = 14501
    	if( 14501 != $form_data['id'] )
    		return $fields;
    
    	// Calculates total entries in a particular form and adds one to it
    	$fields[3]['value'] = wpforms()->entry->get_entries( array( 'form_id' => 14501 ), true ) + 1;
    
    	return $fields;
    }
    add_filter( 'wpforms_process_filter', 'be_wpforms_update_total_field', 10, 3 );
    
    Plugin Support Ethan Choi

    (@ethanchoi)

    Hi @theparzival,

    We’re glad to hear you’ve got that sorted, and thanks for sharing the details with us!

    Have a good one 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add Incremental ID to Individual Forms’ is closed to new replies.