squalljustice
Forum Replies Created
Viewing 1 replies (of 1 total)
-
Hello, i tried this code from patrick :
<?php if ( ! class_exists( 'WPMUDEV_Forminator_Random_Number_Generator' ) ) { class WPMUDEV_Forminator_Random_Number_Generator { private $length = 5; private $form_id = array(2910, 2878, 1633); private $form_field = 'hidden-1'; private $email_macro = '{random_number}'; private static $_instance = null; private $generations = 0; private $random_number = ''; private $current_form = ''; public static function get_instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new WPMUDEV_Forminator_Random_Number_Generator(); } return self::$_instance; } private function __construct() { $this->init(); } public function init() { add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_randomize_hidden_input' ), 10, 2 ); add_filter( 'forminator_replace_variables', array( $this, 'forminator_replace_variables' ), 10, 2 ); } public function forminator_replace_variables( $content, $content_before_replacement ) { return str_replace( $this->email_macro, $this->random_number, $content ); } public function wpmudev_randomize_hidden_input( $field_data_array, $form_id ) { foreach ($this->form_id as $form_ids){ if ( (int) $form_id == (int) $form_ids ) { $this->current_form = $form_id; $this->random_number = $this->wpmudev_randomizer(); $incremental = get_post_meta( $form_ids, 'autoincrement_int', true ); if ( ! $incremental ) { $incremental = 1; } else { $incremental = $incremental + 1; } update_post_meta( $form_ids, 'autoincrement_int', $incremental ); $field_data_array[] = array( 'name' => $this->form_field, 'value' => $this->random_number, ); } } return $field_data_array; } public function wpmudev_is_unique( $num = '' ) { global $wpdb; $query = "SELECT COUNT(*) FROM {$wpdb->prefix}frmt_form_entry_meta WHERE meta_key LIKE '{$this->form_field}' AND meta_value LIKE '{$num}' AND entry_id IN( SELECT entry_id FROM {$wpdb->prefix}frmt_form_entry WHERE form_id IN (" . implode(',', $this->form_id) . ") );"; if ( 0 === (int) $wpdb->get_var( $query ) ) { return true; } else { $this->generations++; return false; } } public function wpmudev_randomizer() { $num = ''; $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for ( $i = 0; $i < $this->length; $i++ ) { $num .= $characters[ rand( 0, strlen( $characters ) - 1 ) ]; } if ( $this->generations > pow( strlen( $characters ), $this->length ) ) { return null; } if ( empty( $num ) ) { return null; } $rand_num = get_post_meta( $this->current_form, 'autoincrement_int', true ); if ( ! $rand_num ) { $rand_num = 1; } else { $rand_num = $rand_num + 1; } if ( $this->wpmudev_is_unique( '#'.$num.$rand_num ) ) { return '#'.$num.$rand_num; } else { return $this->wpmudev_randomizer(); } } } if ( ! function_exists( 'wpmudev_forminator_random_number_generator' ) ) { function wpmudev_forminator_random_number_generator() { return WPMUDEV_Forminator_Random_Number_Generator::get_instance(); }; add_action( 'plugins_loaded', 'wpmudev_forminator_random_number_generator', 10 ); } }Its working great, i changed length to 6 and i have results in mail like expected.
Result is in this form : #MJWSAY2
Problem is that i would need to have the same result but only numbers instead of letters and without #, because i want to put this {random_number} in pre-generated link for online payment, but it only accept numbers, and i’m not experienced enough to modify this part of the code.
Thanks by advance if you can do anything to this.
- This reply was modified 3 years, 1 month ago by squalljustice.
Viewing 1 replies (of 1 total)