Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfredo-wpmudev/99b781a5fd938587ad04177210ec8424 to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/99b781a5fd938587ad04177210ec8424 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
// Add your form IDs here.
$form_ids = array( 842 );
// Change this to the message that you want to show.
$message = 'You cannot submit more than 5 times within 30 days.';
if ( in_array( intval( $form_id ), $form_ids, true ) ) {
$user_ip = Forminator_Geo::get_user_ip();
if ( ! empty( $user_ip ) ) {
$last_entry = wpmudev_get_last_few_entries( $form_id, $user_ip );
if ( ! empty( $last_entry ) ) {
$entry = Forminator_API::get_entry( $form_id, $last_entry );
$current_time = strtotime( date( 'Y-m-d H:i:s' ) );
$future_time = strtotime( '+2 minutes', strtotime( $entry->date_created_sql ) );
if ( $current_time < $future_time ) {
$submit_errors[]['submit'] = $message;
}
}
}
}
return $submit_errors;
},15,3);
function wpmudev_get_last_few_entries( $form_id, $user_ip ){
global $wpdb;
$table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META );
$entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY );
$sql = "SELECT m.`entry_id` FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.`entry_id` = m.`entry_id`) WHERE e.`form_id` = %d AND m.`meta_key` = %s AND m.`meta_value` = %s order by m.`meta_id` desc limit 4,5";
$entry_id = $wpdb->get_var( $wpdb->prepare( $sql, $form_id, '_forminator_user_ip', $user_ip ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( $entry_id ) {
return $entry_id;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment