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 );
}
}
}
Close forms on the weekend
Use this code to close forms on the weekend.
add_action( 'frm_display_form_action', 'frm_close_form_on_weekends', 8, 3 );
function frm_close_form_on_weekends( $params, $fields, $form ) {
remove_filter( 'frm_continue_to_new', '__return_false', 50 );
if ( in_array( $form->id, array( 214, 216, 217 ) ) && ! is_admin() ) { //replace 214, 216, 217 with the ids of the forms you'd like to close on weekends
/**
* 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/Los_Angeles' ); //replace America/Los_Angeles with the timezone of your choice https://www.php.net/manual/en/timezones.america.php
$date = new DateTime;
$date->setTimezone( $timezone );
$day_of_week = $date->format( 'w' );
if ( in_array( (int)$day_of_week, array( 0, 6 ) ) ) {
$message = 'This form is closed on weekends. Check back with us during the week.'; // Any shortcodes added in $message will be rendered by do_shortcode
echo do_shortcode( $message );
add_filter( 'frm_continue_to_new', '__return_false', 50 );
}
}
}
- Change 214, 216, 217 to the id or ids of the forms you want to close on weekends.
- Change America/Los_Angeles to the timezone you'd like to use to determine which day it is.
- The code closes the form on Saturday and Sunday by default.
If you'd like the form closed on different days, change 0, 6 to the days on which you want the form closed. Days of the week are numbered, starting with Sunday as 0 and ending with Saturday as 6. - Change the message to the message you'd like to show when the form is closed. You can include shortcodes in your message, if you like.
Open forms on certain days and times
Use this code to determine the hours and days when a form is open.
You can close the form completely on some days. The code closes forms on Saturday and Sunday by default.
You can open the form only for certain hours on some days by listing the start and end times and the days where those apply. The code opens the form from 9:30 am to 6:00 pm by default.
If a day isn't listed in either list, the form will be open the full day that day.
add_action( 'frm_display_form_action', 'frm_open_hours_and_days', 8, 3 );
function frm_open_hours_and_days( $params, $fields, $form ) {
remove_filter( 'frm_continue_to_new', '__return_false', 50 );
if ( ! in_array( $form->id, array( 214, 216, 217 ) ) || is_admin() ) { //replace 214, 216, 217 with the ids of the forms you'd like to close on weekends
return;
}
/**
* Using DateTime rather than gmdate will provide a locale
* aware interpretation of the date, allowing us to generate
* a timestamp that takes the GMT offset into account
*/
$timezone = new DateTimeZone( 'America/Los_Angeles' ); //replace America/Los_Angeles with the timezone of your choice https://www.php.net/manual/en/timezones.america.php
$now = new DateTime;
$now->setTimezone( $timezone );
$day_of_week = $now->format( 'w' );
$open = true;
if ( in_array( (int) $day_of_week, array( 0, 6 ) ) ) { // Change 0, 6 to a list of the days where the form is closed.
$open = false;
}
$start = new DateTime;
$start->setTimezone( $timezone );
$start->setTime( 9, 30 ); //Change 9 to the hours and 30 to the minutes of the opening time, using a 24-hour clock.
$end = new DateTime;
$end->setTimezone( $timezone );
$end->setTime( 18, 0 ); // Change 18 to the hours and 0 to the minutes of the closing time, using a 24-hour clock.
if ( $open && in_array( (int) $day_of_week, array( 1, 2, 3, 4, 5 ) ) ) { // Change 1, 2, 3, 4, 5 to a list of the days where the form is open only during certain hours.
if ( $now > $end || $now < $start ) {
$open = false;
}
}
if ( ! $open ) {
$message = 'This form is closed now. Please check back with us on week days between 9:30 am and 6:00 pm.'; // Any shortcodes added in $message will be rendered by do_shortcode
echo do_shortcode( $message );
add_filter( 'frm_continue_to_new', '__return_false', 50 );
}
}
- Change 214, 216, 217 to the id or ids of the forms whose opening hours and days you'd like to control with this code example.
- Change America/Los_Angeles to the timezone you'd like to use to determine which day it is.
- Change 0, 6 to the days on which you want the form closed. Days of the week are numbered, starting with Sunday at 0 and ending with Saturday at 6.
- Change 9 and 30 to the hour and minutes you want the form(s) to open, using a 24-hour clock.
- Change 18 and 0 for the hours and minutes you want the form(s) to be closed, using a 24-hour clock.
- Change 1, 2, 3, 4, 5 to the days on which you want the form open during certain hours.
- If you want the forms to be open a full day on some days, don't include those days in either of the lists described above.
- Change the message to the message you'd like to show when the form is closed. You can include shortcodes in your message, if you like.