Limit User to One Submission Per Time Period

The functionality in this snippet has been enhanced. I strongly recommend you use the “better” version: Better Limit Submission Per Time Period by User or IP

Would you like to limit your Gravity Form to only allow one submission per time period? Say for instance the user should only be able to submit the form once every 24 hours? This snippet will do the trick!

/**
* Limit User to One Submission Per Time Period
* http://gravitywiz.com/2012/04/25/limit-user-to-one-submission-per-time-period/
*/

// update the "5" to your form ID
add_action('gform_pre_render_5', 'gform_limit_submissions');
function gform_limit_submissions($form){
    global $wpdb;
    
    $limit_message = 'You may only submit this form once every 24 hours.';
    $limit_time = 86400; // must be specified in seconds; 86400 seconds is equal to 24 hours
    
    /* You do not need to edit below this line */
    
    $current_user = wp_get_current_user();
    $last_submission = $wpdb->get_var($wpdb->prepare("SELECT date_created FROM {$wpdb->prefix}rg_lead WHERE created_by = %d and form_id = %d ORDER BY date_created DESC", $current_user->ID, $form['id']));
    
    if(empty($last_submission))
        return $form;
    
    $time_out = strtotime($last_submission) + $limit_time;
    $current_time = time();
    
    if($current_time > $time_out)
        return $form;
    
    $is_submit = rgpost("is_submit_{$form['id']}");
    
    if(!$is_submit) {
        add_filter('gform_get_form_filter', create_function('', "return '<div class="limit-message">$limit_message</div>';") );
    }
    
    return $form;
}
view raw gistfile1.php This Gist brought to you by GitHub.

How do I install this snippet?

Just copy and paste the code into your theme’s functions.php file.

Do I need to modify this snippet to work with my form?

Be sure to update the form ID and the $limit_message and $limit_time variables as indicated in the code.

A more generally usable version of this snippet might limit based on IP rather than WordPress user ID. Let us know if this is something that would interest you!

Share the Magic

Support Gravity Wiz

Comments

  1. what about submissions that dont require a user to be logged in?

     

    im using gravityforms on a site that creates a post, and they dont have to be logged in to do so.

  2. cool thanks, i was setting a 24 hour cookie and checking that via the pre submission

  3. How would you limit to one entry per time period, per email address?

    I have a form that the user enters name and email address, but I want it to limit it to one entry per day per email address (or alternatively one entry per 24 hours per email address will work).

    GREAT site here!

    • Hi Bruce,

      This would be a bit more complicated. Submit it as a question to Ask Wiz and it will be in the pool for questions I pick from. :D

  4. Hi David,

    I have submitted my question to Ask Wiz and am hoping it will be selected. :)

    Thanks much!

  5. This is great, I am a first time visitor to the site.. awesome!

    My question is..for a noob.. where in the world am I adding this snippet?  THANKS!

  6. Nice code. 2 Questions:

    1) What if we are collecting a email on a form and want to limit 24 hours between submissions based on the email that is provided? I can handle the SQL change but how do you get the field contents of a form (i.e., email) to compare it to?

    2) Is it possible to display how much time is remaining until the next submission?

    Thanks!

  7. Really helpful code here.  Getting PHP error on line 28 for some reason.

    • Hi Leland,

      There’s a bit of an issue with the code I’m using to output snippets. I’ve updated the snippet so the output is now correct and the quotes are probably escaped on the line you referenced. The copy to clipboard feature is not working so you’ll need to copy it manually.

  8. This is amazing!  Got it working to check for user posts submitted limits and user role checking.  Awesome blog.  This is just what the GF needs.  Interested in my code?

  9. This is exactly what I’m after, but for some reason, it doesn’t restrict the use, I can keep filling in forms.

    I changed the form ID, and have also tried with a non-admin account, just in case that was the problem.

    Thanks.

    • Talked with Joe via email. There was an issue with his file not saving.

      Glad you were able to get this sorted Joe!

  10. Sorry for the stupid question…I’m new here.

    But where do I put this code? Which php file?

    Thanks a lot

    • Hi Ryan, looks like I forgot to include that info on this snippet! I’ve updated the post now. You can just copy and paste the code into your theme’s functions.php file.