This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook allows you to do something before the form is displayed, such as checking the number of entries and displaying a message if the number of entries has reached its limit.
Usage
add_action('frm_display_form_action', 'check_entry_count', 8, 3); function check_entry_count($params, $fields, $form)
Parameters
- $params (array)
- $fields (array)
- $form (object)
Examples
Limit total number of entries
Use this code to limit the total number of entries that can be submitted for a particular form. This code will check if your entry limit has been reached and if it has, then the form will not be displayed.
add_action('frm_display_form_action', 'limit_entry_count', 8, 3);
function limit_entry_count($params, $fields, $form){
remove_filter('frm_continue_to_new', '__return_false', 50);
if ( in_array($form->id, array(5,6,7)) ) { //change 5, 6, and 7 to your form ID(s)
$count = FrmEntry::getRecordCount($form->id);
if ( $count >= 15 ) { //change 15 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Limit the number of entries per user
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 5 and !is_admin()){ //replace 5 with the ID of your form
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);
if($count >= 2){ //change 2 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Limit the number of entries per IP
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 5 and !is_admin()){ //replace 5 with the ID of your form
$count = FrmEntry::getRecordCount( array('form_id' => $form->id, 'it.ip' => $_SERVER['REMOTE_ADDR'] ) );
if($count >= 2){ //change 2 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Limit logged-in users to two entries per day
NOTE: You may need to customize this code to work for your timezone
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
remove_filter('frm_continue_to_new', '__return_false', 50);
global $user_ID;
if($form->id == 5 and !is_admin()){ //replace 5 with your form ID
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND it.created_at > '". gmdate('Y-m-d')." 00:00:00' AND user_id=".$user_ID);
//$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND it.created_at > '". gmdate('Y-m')."-01 00:00:00' AND user_id=".$user_ID); // uncommented this line to limit by month instead of day
if($count >= 2){ //change 2 to your limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Limit Logged in users to two entries per 24 hour period
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 5 and !current_user_can('administrator')) { //change 5 to the form ID
$one_day_ago = strtotime(current_time('mysql', 1)) - (60*60*24); //calculate 24 hours ago
$count = FrmEntry::getRecordCount("it.created_at > '".date('Y-m-d H:i:s', $one_day_ago)."' and user_id=".$user_ID);
if($count >= 2){
echo 'Sorry - only two entries per 24hrs per user - please try again later ...';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Close the form on a specific date
Only show your form before a specified date. This example closes the form on 2012-05-15.
add_action('frm_display_form_action', 'close_my_form', 8, 3);
function close_my_form($params, $fields, $form){
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 6){ //remove this section or change 6 to a form ID
if(time() > strtotime('2012-05-15')){
echo 'This form has expired';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Only show form to logged-out users
add_action('frm_display_form_action', 'close_my_form', 8, 3);
function close_my_form($params, $fields, $form){
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 6){ //remove this section or change 6 to a form ID
if(is_user_logged_in()){
echo 'You are logged in';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}
Show a message when trying to edit
If someone changes the id of the entry in a url to edit, they will see a blank form. This example will show a message instead of the empty form.
add_action('frm_display_form_action', 'close_my_form', 20, 3);
function redirect_edit_form( $params, $fields, $form ) {
global $frm_vars;
$edit_attempt = isset( $_GET['entry'] );
$editing_entry = isset( $frm_vars['editing_entry'] ) ? $frm_vars['editing_entry'] : 0;
if ( $edit_attempt && ! $editing_entry ) {
echo 'You do not have permission to edit that entry.';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
Limit submissions by any stat
You can use stats to determine whether a form should be open.
- Change 194 to the id of your form.
- Change 893 to the id of a field that always has content in your form.
- Change the other params like 'type'=>'count', 'user_id'=>'current', and 'created_at_greater_than'=>'-4 months" to the params you'd like to use in your stat.
- Change 1 to the limit you want to use.
- Change the message "You have already submitted this form in the last four months" to the message you want to display
add_action( 'frm_display_form_action', 'limit_entry_by_stat', 8, 3 );
function limit_entry_by_stat( $params, $fields, $form ) {
remove_filter( 'frm_continue_to_new', '__return_false', 50 );
if ( $form->id == 194 ) { //change 194 to your form ID
$count = FrmProStatisticsController::stats_shortcode( array( 'id' => 893,
'type' => 'count',
'user_id' => 'current',
'created_at_greater_than' => "-4 months"
) );
if ( $count >= 1 ) { //change 2 to your entry limit
echo 'You have already submitted this form in the last four months.';
add_filter( 'frm_continue_to_new', '__return_false', 50 );
}
}
}
This is an alternate approach to many of our other code examples for frm_display_form_action.
Modify time
Modify time when limiting tickets to one per day
add_action( 'frm_display_form_action', 'ff_enforce_single_daily_entry', 8, 3 );
function ff_enforce_single_daily_entry( $params, $fields, $form ) {
global $user_ID;
remove_filter( 'frm_continue_to_new', '__return_false', 50 );
if ( '123' === $form->id && ! is_admin() ) { //replace 123 with the id of the form you are going to work with
/**
* Using DateTime rather than gmdate will provide a locale
* aware interpretation of the date, allowing us to generate
* a timestampt that takes the GMT offset into account
*/
$timezone = new DateTimeZone( 'America/Denver' ); //replace America/Denver with the timezone of your choice https://www.php.net/manual/en/timezones.america.php
$time = new DateTime( 'today midnight', $timezone );
$time->setTimezone( new DateTimeZone( 'UTC' ) );
$timestamp = $time->format( 'Y-m-d H:i:s' );
$query = sprintf( "it.form_id=%d AND it.is_draft = '0' AND user_id = %d AND it.created_at > '%s'", $form->id, sanitize_text_field( $user_ID ), $timestamp );
$count = FrmEntry::getRecordCount( $query );
if ( $count >= 1 ) {
//do what you want here, eg: add a shortcode
$message = '[custom_shortcode]'; // Any shortcodes added in $message will be rendered by do_shortcode
echo do_shortcode( $message );
add_filter( 'frm_continue_to_new', '__return_false', 50 );
}
}
}