How to Limit submissions per time period (once a month)

By: Sina Pars | Asked: 04/27/2024
ForumsCategory: Code SnippetsHow to Limit submissions per time period (once a month)
Sina Pars asked 5 months ago
Hi there, I am using the following filter to limit the forms registered per month so that each user (based on email) can only fill the form once per month.
But unfortunately, this restriction is only applied on the same day and the user can register the form again the next day
yours document:
https://formidableforms.com/knowledgebase/frm_validate_entry/#kb-limit-submissions-per-time-period-or-any-stat
Is this filter still valid?
My code is:
Field 182 is the email field of my users `
add_filter('frm_validate_entry', 'check_submitted', 20, 2);
function check_submitted($errors, $values){
	if  ( $values['form_id'] !== 6 ) {//Change 30 to the ID of your form 
		return $errors;
	}
	
	$entries_submitted = FrmProStatisticsController::stats_shortcode( array( 'id' => 182, 'type' => 'count', 'user_id' => 'current',  'created_at_greater_than' => 'this month' ) );//change the params to the params of your stat

	if ( $entries_submitted >= 1 ){//change 1 to your limit number
		$errors['too_many'] = 'You filled this form this month, please register the feedback in the next month.';//Change this to your error message
	}

	return $errors;
}
`  
2 Answers
Best Answer
Bobby Clapp answered 5 months ago

Let us look at 'created_at_greater_than' => 'this month' part of the code you shared. Does that make any sense? I think the first part is botching your request. To limit to the current month you should not be trying to count entries that are ultimately created at "greater_than" this month, correct?

According to the stats_shortcode parameters seen here: https://formidableforms.com/knowledgebase/add-field-totals-and-statistics/ we only have "created_at_greater_than" and "created_at_less_than" to work with. Let's see if incorporating that logical language into the code solves the problem.

'created_at_greater_than' => 'first day of this month', 'created_at_less_than' => 'last day of this month'

Here we are counting only the entries that have a created date of "greater than" the first day of this month and then also a created date of "less than" the last day of this month.

Logically that should count all entries within the current month and return the value you are expecting.

Sina Pars answered 5 months ago
Hi @dgodfather, Thank you for your help and guidance I tweaked my code based on your advice, and it looks like the problem is fixed, just to make sure I have to try after the end of this month if the new form will be registered or not, I guess it shouldn't be a problem. BR
Bobby Clapp replied 5 months ago

Awesome. Yeah, all code needs thorough testing.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right