Last updated on October 18, 2023

WP Forms form submission limits

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Limit the number of submissions in WP Forms.

The code snippet below demonstrates how to set a submission limit for a WP Forms form. This can be useful in scenarios where you want to restrict the number of times a form can be submitted, such as for limited-time offers or exclusive events.

function wpsnippets_limit_form_submissions( $form_data ) {
    $form_id = $form_data['id'];
    $submission_limit = 10; // Set the desired submission limit here

    $submission_count = get_option( 'wpsnippets_form_submission_count_' . $form_id, 0 );

    if ( $submission_count >= $submission_limit ) {
        // Disable the form if the submission limit is reached
        $form_data['settings']['form_is_active'] = false;
    } else {
        // Increment the submission count
        $submission_count++;
        update_option( 'wpsnippets_form_submission_count_' . $form_id, $submission_count );
    }

    return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'wpsnippets_limit_form_submissions' );

In this code snippet, we define a custom function wpsnippets_limit_form_submissions that takes the form data as a parameter. Inside the function, we retrieve the form ID and the desired submission limit. We then retrieve the current submission count from the WordPress options table using the form ID as the key.

If the submission count exceeds or equals the submission limit, we disable the form by setting the form_is_active setting to false. Otherwise, we increment the submission count and update it in the options table.

Finally, we hook the wpsnippets_limit_form_submissions function to the wpforms_frontend_form_data filter, which allows us to modify the form data before it is processed.

Note: This code assumes you have WP Forms plugin installed and activated on your WordPress site.

Examples

Example 1: Limiting the number of form submissions per user

This use case demonstrates how to limit the number of form submissions a user can make using WP Forms. The code example below uses a custom PHP function wpsnippets_limit_form_submissions() that checks the number of submissions made by a user and prevents further submissions if the limit is reached.

function wpsnippets_limit_form_submissions($form_id, $limit) {
    $user_id = get_current_user_id();
    $submission_count = wpsnippets_get_submission_count($form_id, $user_id);

    if ($submission_count >= $limit) {
        wp_die('You have reached the maximum number of form submissions.');
    }
}

add_action('wpforms_process_complete', 'wpsnippets_limit_form_submissions', 10, 2);

The wpsnippets_limit_form_submissions() function retrieves the current user’s ID using get_current_user_id(). It then calls another custom function wpsnippets_get_submission_count() to get the number of form submissions made by the user. If the submission count exceeds the specified limit, the function displays an error message using wp_die().

Example 2: Limiting form submissions based on a specific form field value

This use case demonstrates how to limit form submissions based on a specific field value in WP Forms. The code example below uses the wpforms_process_complete action hook to check the value of a field with the ID field_123 and prevents further submissions if the value matches a specific condition.

function wpsnippets_limit_form_submissions($form_id) {
    $form_data = wpforms()->form->get( $form_id );
    $field_value = wpforms()->process->fields->get_field_value( $form_data, 'field_123' );

    if ($field_value === 'example') {
        wp_die('Form submissions with this value are not allowed.');
    }
}

add_action('wpforms_process_complete', 'wpsnippets_limit_form_submissions', 10, 1);

The wpsnippets_limit_form_submissions() function retrieves the form data using wpforms()->form->get() and then uses wpforms()->process->fields->get_field_value() to get the value of the field with the ID field_123. If the field value matches the specified condition (e.g., ‘example’), the function displays an error message using wp_die().

Example 3: Limiting form submissions based on a specific form ID

This use case demonstrates how to limit form submissions based on a specific form ID in WP Forms. The code example below uses the wpforms_process_complete action hook to check the form ID and prevents further submissions if it matches a specific ID.

function wpsnippets_limit_form_submissions($form_id) {
    $allowed_form_ids = array(1, 2, 3);

    if (!in_array($form_id, $allowed_form_ids)) {
        wp_die('Form submissions for this form are not allowed.');
    }
}

add_action('wpforms_process_complete', 'wpsnippets_limit_form_submissions', 10, 1);

The wpsnippets_limit_form_submissions() function checks if the submitted form ID is present in the $allowed_form_ids array using in_array(). If the form ID is not found in the array, the function displays an error message using wp_die().

Last updated on October 18, 2023. Originally posted on December 31, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *