Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created June 6, 2019 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/94427e3f3b5024bf165e5b2fc7e1d0c1 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/94427e3f3b5024bf165e5b2fc7e1d0c1 to your computer and use it in GitHub Desktop.
Forminator - Limit form submissions per IP for 24 hours.
<?php
/**
* Plugin Name: Forminator
* Description: Limit form submissions per IP for 24 hours.
* Author: Konstantinos Xenos @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
add_filter(
'forminator_custom_form_submit_errors',
function( $submit_errors, $form_id, $field_data_array ) {
// Add your form IDs here.
$form_ids = array( 49, 15, 20 );
// Change this to the message that you want to show.
$message = 'You cannot submit more than 1 time within 24 hours.';
if ( in_array( intval( $form_id ), $form_ids, true ) ) {
$user_ip = Forminator_Geo::get_user_ip();
if ( ! empty( $user_ip ) ) {
$last_entry = Forminator_Form_Entry_Model::get_last_entry_by_ip_and_form( $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( '+1 day', strtotime( $entry->date_created_sql ) );
if ( $current_time < $future_time ) {
$submit_errors[]['submit'] = $message;
}
}
}
}
return $submit_errors;
},
15,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment