Formidable Forms

The Most Advanced WordPress Forms Plugin and Form Builder

  • Features
  • Pricing
  • Blog
  • Support
  • Login
  • Get Formidable Forms

frm_display_form_action

Last Updated: May 7, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Form Appearance → frm_display_form_action
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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 );
        }
    }
}
  • Usage
  • Parameters
  • Examples
    • Limit total number of entries
    • Limit the number of entries per user
    • Limit the number of entries per IP
    • Limit logged-in users to two entries per day
    • Limit Logged in users to two entries per 24 hour period
    • Close the form on a specific date
    • Only show form to logged-out users
    • Show a message when trying to edit
    • Limit submissions by any stat
    • Modify time
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms
Install Formidable Lite

Using WordPress and want to get Formidable Forms for free?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Front End Editor
  • Repeating Fields
  • Views from Entries
  • Calculator Forms
  • Conditional Logic
  • Visual Form Styler
  • Form Templates
  • User Submitted Posts
  • File Upload Forms
  • Spam Protection
  • Multi Page Forms
  • Surveys & Polls
  • Form Graphs & Charts
  • Save and Continue
  • Mobile Forms
  • Stripe Forms
  • PayPal Forms
  • WooCommerce Forms
  • MailChimp Forms
  • User Registration
  • Signature Forms
  • Bootstrap Forms
  • Quiz Maker
  • Zapier Forms
  • Salesforce Forms
  • HubSpot Forms
  • API Webhooks
  • Multilingual Forms
  • Directories

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • Blog

Copyright © 2021 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews