spivurno
9/13/2012 - 12:19 AM

Gravity Wiz // Limit User to One Submission Per Time Period

Gravity Wiz // Limit User to One Submission Per Time Period

/**
* 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; 
}