Advertisement
adczk

Forminator - duplicate checks with skip option

Mar 22nd, 2023 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Plugin Name: [Forminator] Check submission duplicated data
  5. * Plugin URI: https://premium.wpmudev.org/
  6. * Description: check if entry with same data (per defined field) already exists and prevent such duplicated data submission
  7. * Author: adczk
  8. * Author URI: https://premium.wpmudev.org/
  9. * License: GPLv2 or later
  10. *
  11. * Tested with Forminator 1.22.1
  12. *
  13. *
  14. * USE AS MU PLUGIN
  15. *
  16. * config explained in code comment
  17. *
  18. */
  19.  
  20. add_filter( 'forminator_custom_form_submit_errors', 'check_form_duplicate_submission_data', 99, 3 );
  21. function check_form_duplicate_submission_data( $submit_errors, $form_id, $field_data_array ) {
  22.    
  23.     $form_ids = array( 2245, 123 ); // form IDs - one or more; if more - separate numbers with commas
  24.     $check_field_id = 'checkbox-1'; // ID of the field to determine if to do the data check or skip it; must be SINGLE checkbox
  25.     $fields_ids = array( 'phone-1', 'email-1' ); // IDs of fields to check, one or more, if more - separate with commas
  26.     $err_msg = 'Duplicated entry!'; // custom error message
  27.    
  28.    
  29.    
  30.     // do not edit below
  31.    
  32.     $check = false;
  33.    
  34.     if ( !in_array( $form_id, $form_ids ) ) {
  35.         return $submit_errors; // just bail out and skip checks
  36.     }
  37.    
  38.    
  39.     foreach( $field_data_array as $key => $value ) {
  40.         $field_name = $value['name'];
  41.        
  42.         if ( $field_name == $check_field_id )  {
  43.             $check = true;
  44.         }
  45.                
  46.             if ( in_array( $field_name, $fields_ids ) && $check ) {
  47.            
  48.                 $entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_name, $value['value'], '*' );
  49.                        
  50.        
  51.                 if ( $entries ) {
  52.                     $submit_errors[][$field_name] = $err_msg;
  53.                 }
  54.             }
  55.        
  56.     }
  57.  
  58.     return $submit_errors;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement