Skip to content

Instantly share code, notes, and snippets.

@adczk
Created February 8, 2023 15:57
Show Gist options
  • Save adczk/728fff7d4061eff16bcd60593eba337d to your computer and use it in GitHub Desktop.
Save adczk/728fff7d4061eff16bcd60593eba337d to your computer and use it in GitHub Desktop.
Forminator - validate if same data for given fields was already submitted and prevent duplicated submission
<?php
/**
* Plugin Name: [Forminator] Check submission duplicated data
* Plugin URI: https://premium.wpmudev.org/
* Description: check if entry with same data (per defined field) already exists and prevent such duplicated data submission
* Author: adczk
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*
* Tested with Forminator 1.22.1
*
*
* USE AS MU PLUGIN
*
* config explained in code comment
*
*/
add_filter( 'forminator_custom_form_submit_errors', 'check_form_duplicate_submission_data', 99, 3 );
function check_form_duplicate_submission_data( $submit_errors, $form_id, $field_data_array ) {
$form_ids = array( 2245, 123 ); // form IDs - one or more; if more - separate numbers with commas
$fields_ids = array( 'phone-1' ); // IDs of fields to check, one or more, if more - separate with commas
$err_msg = 'Duplicated entry!'; // custom error message
if ( !in_array( $form_id, $form_ids ) ) {
return $submit_errors; // just bail out and skip checks
}
foreach( $field_data_array as $key => $value ) {
$field_name = $value['name'];
if ( in_array( $field_name, $fields_ids ) ) {
$entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_name, $value['value'], '*' );
if ( $entries ) {
$submit_errors[][$field_name] = $err_msg;
}
}
}
return $submit_errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment