Last updated on October 18, 2023

WP Forms entry limits

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

Limit the number of entries in WP Forms.

The code snippet provided below demonstrates how to limit the number of entries submitted through WP Forms plugin. This can be useful in scenarios where you want to restrict the number of form submissions, such as for event registrations or limited-time offers.

/**
 * Limit the number of form entries in WP Forms.
 *
 * @param bool $is_available Whether the form is available for submission.
 * @param array $form_data The form data.
 * @return bool
 */
function wpsnippets_limit_form_entries( $is_available, $form_data ) {
    // Set the maximum number of entries allowed.
    $max_entries = 100;

    // Get the form ID.
    $form_id = $form_data['id'];

    // Get the number of entries submitted for the form.
    $entry_count = wpforms()->entry->get_entries_count( $form_id );

    // Check if the maximum number of entries has been reached.
    if ( $entry_count >= $max_entries ) {
        $is_available = false;
    }

    return $is_available;
}
add_filter( 'wpforms_frontend_is_form_available', 'wpsnippets_limit_form_entries', 10, 2 );

In this code snippet, we define a custom function wpsnippets_limit_form_entries that hooks into the wpforms_frontend_is_form_available filter. This filter allows us to modify the availability of a form for submission.

Inside the function, we set the maximum number of entries allowed by assigning a value to the $max_entries variable. You can adjust this value according to your requirements.

Next, we retrieve the form ID from the $form_data array. This array contains information about the form being submitted.

We then use the wpforms()->entry->get_entries_count() function to get the number of entries submitted for the form. This function takes the form ID as a parameter and returns the entry count.

Finally, we check if the entry count exceeds the maximum allowed entries. If it does, we set the $is_available variable to false, indicating that the form is not available for submission.

Remember to replace wpsnippets_ with your own custom prefix to ensure uniqueness of the function name.

By implementing this code snippet, you can easily limit the number of entries submitted through WP Forms plugin.

Examples

Example 1: Limiting the number of form entries per user

This example demonstrates how to limit the number of form entries a user can submit using WP Forms. The code snippet checks the number of entries submitted by the current user and prevents them from submitting more than a specified limit.

function wpsnippets_limit_form_entries( $form_id, $entry_limit ) {
    $user_id = get_current_user_id();
    $entry_count = wpforms()->entry->get_entries(
        array(
            'form_id' => $form_id,
            'user_id' => $user_id,
        )
    );

    if ( count( $entry_count ) >= $entry_limit ) {
        wpforms()->process->errors[ $form_id ]['form'] = 'You have reached the maximum number of entries.';
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_limit_form_entries', 10, 2 );

This code snippet uses the wpforms_process_complete action hook to check the number of entries submitted by the current user for a specific form. If the user has reached the entry limit, an error message is added to the form submission process, preventing further submissions.

Example 2: Limiting the number of form entries globally

In this example, we limit the number of form entries globally, regardless of the user. The code snippet checks the total number of entries for a specific form and prevents any new submissions once the limit is reached.

function wpsnippets_limit_form_entries( $form_id, $entry_limit ) {
    $entry_count = wpforms()->entry->get_entries(
        array(
            'form_id' => $form_id,
        )
    );

    if ( count( $entry_count ) >= $entry_limit ) {
        wpforms()->process->errors[ $form_id ]['form'] = 'This form has reached the maximum number of entries.';
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_limit_form_entries', 10, 2 );

This code snippet is similar to the previous example, but it doesn’t check for the user ID. Instead, it counts all the entries for a specific form and prevents new submissions once the entry limit is reached.

Example 3: Limiting the number of form entries based on a specific field value

In this example, we limit the number of form entries based on a specific field value. The code snippet checks the value of a field in each entry and prevents new submissions once the limit is reached for that specific value.

function wpsnippets_limit_form_entries( $form_id, $field_id, $field_value, $entry_limit ) {
    $entry_count = wpforms()->entry->get_entries(
        array(
            'form_id' => $form_id,
            'field_id' => $field_id,
            'field_value' => $field_value,
        )
    );

    if ( count( $entry_count ) >= $entry_limit ) {
        wpforms()->process->errors[ $form_id ]['form'] = 'The maximum number of entries for this field value has been reached.';
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_limit_form_entries', 10, 4 );

This code snippet allows you to limit the number of form entries based on a specific field value. It checks the value of a field in each entry and prevents new submissions once the entry limit is reached for that specific value. You need to provide the form ID, field ID, field value, and the entry limit as parameters when calling the function.

Last updated on October 18, 2023. Originally posted on October 19, 2023.

Leave a Reply

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