Auto increase field by 1
-
Hello, i need a field to be increased by 1 after every successful entry of a form. also i need this field to has its own sequential number for each user who will submit the form. i have found a code in another posts , https://gist.github.com/adczk/fbf06b6abf7753df6b37234b13041e79 it works but i need this to be unique for each logged in user who submits the form. Thank you!
-
Hello @skoyntoyflis
Hope you’re doing well today! Thank you for reaching out to us.
Regarding your query, since it requires additional checks for the users to be logged in, can you please confirm if the form is supposed to be submitted only by the logged-in users? Or non-logged in users can submit it too?
With that said, I am checking with our developers if we can suggest a workaround for this use case, we will update you here as soon as we have further insights from them.
Kind Regards,
Saurabhhello Saurabh, thank you for your reply.
yes the form will be submitted only by logged in users.
thank you!
Hi @skoyntoyflis,
Thanks for the clarification. Then we will assume that the form is already setup as Logged-In Submission?Only
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#enable-logged-in-submission-only
Kind regards,
Zaferhello, until you reply to my previous question, i would like to ask you something else.
i need to add choices on a select field of the form from a custom post type “costumers” the acf custom field with the slug “name”, how can i do it?
thank you!
I hope you are doing well today.
<?php add_action( 'after_setup_theme', 'wpmudev_forminator_autoincreased_field', 100 ); function wpmudev_forminator_autoincreased_field() { if ( class_exists( 'Forminator' ) ) { class WPMUDEV_FM_Autoincreased_field { // CONFIGURATION ############### private $macro = '{formatted_value}'; // The text to replace in the Thank you message (fixed by AMIT) private $field_name = 'hidden-1'; // id of the field to hold autoincrement value; must be existing field of type hidden private $form_ids = array( 21683 ); // comma separated list of forms to use this with private $start_number = 1; // starting number to count up from private $prefix = 'CT2021_'; // field value prefix private $suffix = ''; // field value suffix private $leading_zeros = 2; // how many leading zeros; // 0: number are like 1, 2, 3, 4, 5 and so on // 1: number are like 01, 02, 03...10, 11 and so on // 2: numbers are like 001, 002...011, 012, 101, 520 and so on // 3: numbers are like 0001, 002... 0011... 0520, 1234 and so on... // ... // DO NOT EDIT BELOW ###################### private $entry; private $order_numbers; // AMIT: Variable to hold formatted value among filters private $formatted_value; public function __construct() { // reset count (for admin) add_action( 'admin_init', array( $this, 'reset_number' ) ); add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'set_custom_value' ), 10, 2 ); add_filter( 'forminator_custom_form_submit_errors', array( $this, 'pre_set_custom_value' ), 10, 3 ); add_filter( 'forminator_custom_form_mail_data', array( $this, 'wpmudev_fm_mail_form_data' ), 10, 3 ); // handle $macro in notification subject and message add_filter( 'forminator_custom_form_mail_admin_subject', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 ); add_filter( 'forminator_custom_form_mail_admin_message', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 ); // AMIT: Update Formatted value macro in the Thank you response add_filter( 'forminator_form_ajax_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 ); add_filter( 'forminator_form_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 ); } // helper - format value with leading zeros, prefix and suffix public function do_format_value( $value ) { $formatted_value = $value; // set leading zeros $add_zeros = ( $this->leading_zeros + 1 ) - strlen( $value ); if ( $add_zeros > 0 ) { for ( $i = 1; $i <= $add_zeros; $i++ ) { $formatted_value = '0' . $formatted_value; } } if ( is_user_logged_in() ) { $this->prefix = 'submission_'; // field value prefix } // add prefix/suffix $formatted_value = $this->prefix . $formatted_value . $this->suffix; return $formatted_value; } // helper - get clean number by removing prefix, suffix and leading zeros // // NOT USED // // but shows how to "unformat"/"decode" formatted value to a raw number public function do_unformat_value( $value ) { if ( is_user_logged_in() ) { $this->prefix = wp_get_current_user()->user_login . '_'; // field value prefix } $unformatted_value = (int) ltrim( str_replace( $this->suffix, '', str_replace( $this->prefix, '', $value ) ), '0' ); return $unformatted_value; } // helper - get number from DB option public function get_number( $form_id ) { static $raw_number; if ( ! $raw_number ) { $formatted_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); if ( isset( $formatted_numbers[ $form_id ] ) ) { $raw_number = $formatted_numbers[ $form_id ]; } else { $raw_number = $this->start_number; } } return $raw_number; } // reset number function public function reset_number() { if ( current_user_can( 'manage_options' ) && isset( $_GET['wpmudev-fm-reset-number-by-form-id'] ) ) { $form_id = (int) $_GET['wpmudev-fm-reset-number-by-form-id']; if ( $form_id ) { $order_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); $order_numbers[ $form_id ] = $this->start_number; update_option( 'wpmudev_fm_autoincremented_field', $order_numbers ); } } } // get, increase and update value public function pre_set_custom_value( $submit_errors, $form_id, $field_data_array ) { //public function set_custom_value( $field_data_array, $form_id ) { if ( ! in_array( $form_id, $this->form_ids ) ) { return $submit_errors; } if ( !empty( $submit_errors) ) { return $submit_errors; } $current_value = $this->get_number( $form_id ); $order_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); // increase value for next submission $current_value++; $order_numbers[ $form_id ] = $current_value; // and remember it in DB option update_option( 'wpmudev_fm_autoincremented_field', $order_numbers ); //return $field_data_array; return $submit_errors; } public function set_custom_value( $field_data_array, $form_id ) { if ( ! in_array( $form_id, $this->form_ids ) ) { return $field_data_array; } $current_value = $this->get_number( $form_id ); // AMIT: Save Formatted value in class level variable $this->formatted_value = $this->do_format_value( $current_value ); $custom_data_array[] = array( 'name' => $this->field_name, 'value' => $this->formatted_value, // AMIT, changed ); // update form submission data $field_data_array = array_merge( $field_data_array, $custom_data_array ); return $field_data_array; } public function wpmudev_fm_mail_form_data( $data, $custom_form, $entry ) { /* make sure that field variable in notification uses saved data instead of submitted */ $data[ $this->field_name ] = $entry->meta_data[ $this->field_name ]['value']; return $data; } public function wpmudev_fm_mail_macro_replace( $message, $custom_form, $data ) { $message = str_replace( $this->macro, $this->formatted_value, $message ); return $message; } /* ** AMIT: Inject the formatted value in thank you response */ public function inject_formatted_value_in_response( $response ) { if ( isset( $this->formatted_value ) ) { $response['message'] = str_replace( $this->macro, $this->formatted_value, $response['message'] ); } return $response; } } $run = new WPMUDEV_FM_Autoincreased_field(); } }
Please change 21683 to your form ID, and submission_ to your custom prefix. In the end, your hidden field values will look like this as values:
- submission_001
- submission_002
- etc
To learn more about how to install MU-Plugin to a site, please, check this out:?https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
=======
As for your 2nd query do you mean that your form select should read name which has multiple values and it should pick up the one which is selected there for that user?
Kind Regards,
Krisi mean that in select field of the form i need to have choices from the custom post type “customers” the custom field “name” the name of the customer, and depending on that choice, other fields on the form (input fields) to filled up with the details of the specific customer , as phone , address etc.
thank you!
Hi again @skoyntoyflis
It sounds a little bit complex as this will need to read data from database, and in scenario of 500 or 3000 customers it could freeze page or browser. I pinged our SLS Team once more and see how complex this will be and can we help in that matter. Please note some task can be outside of support scope.
Kind Regards,
Krishello, great thank you.
Hi @skoyntoyflis,
Hope this message finds you well.
We got some feedback from our devs, unfortunately, as mentioned by Kris, the last request is too complex and it is out of our support scope.
Best regards,
LauraThank you.
- The topic ‘Auto increase field by 1’ is closed to new replies.