# Close a Contact Form 7 Form After a Number of Entries

> Accepts only a set number of successful submissions for a Contact Form 7 form, such as event places or giveaway entries, then shows a "closed" message instead of the form.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, limits, events
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-limit-total-submissions/

## Code

```php
// Form ID => maximum successful submissions.
$wpide_cf7_limits = array(); // e.g. array( 123 => 50 ).
$wpide_cf7_closed_message = 'Sorry, this form is closed because all places have been taken.';

add_action( 'wpcf7_mail_sent', function ( $contact_form ) use ( $wpide_cf7_limits ) {
    $id = (int) $contact_form->id();
    if ( isset( $wpide_cf7_limits[ $id ] ) ) {
        update_option( 'wpide_cf7_count_' . $id, (int) get_option( 'wpide_cf7_count_' . $id, 0 ) + 1, false );
    }
} );

add_filter( 'wpcf7_validate', function ( $result ) use ( $wpide_cf7_limits, $wpide_cf7_closed_message ) {
    $form = function_exists( 'wpcf7_get_current_contact_form' ) ? wpcf7_get_current_contact_form() : null;
    $id   = $form ? (int) $form->id() : 0;

    if ( isset( $wpide_cf7_limits[ $id ] ) && (int) get_option( 'wpide_cf7_count_' . $id, 0 ) >= $wpide_cf7_limits[ $id ] ) {
        $tags = $form->scan_form_tags();
        if ( $tags ) {
            $result->invalidate( $tags[0], $wpide_cf7_closed_message );
        }
    }

    return $result;
} );

add_filter( 'do_shortcode_tag', function ( $output, $tag, $attr ) use ( $wpide_cf7_limits, $wpide_cf7_closed_message ) {
    if ( 'contact-form-7' !== $tag || empty( $attr['id'] ) ) {
        return $output;
    }

    $form = function_exists( 'wpcf7_contact_form' ) ? wpcf7_contact_form( $attr['id'] ) : null;
    $id   = $form ? (int) $form->id() : 0;

    if ( isset( $wpide_cf7_limits[ $id ] ) && (int) get_option( 'wpide_cf7_count_' . $id, 0 ) >= $wpide_cf7_limits[ $id ] ) {
        return '<p class="wpide-form-closed" role="status">' . esc_html( $wpide_cf7_closed_message ) . '</p>';
    }

    return $output;
}, 10, 3 );
```

## Notes

Add form IDs and limits to the list at the top; the count is stored per form in the option wpide_cf7_count_<ID>, which you can delete to reopen the form.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Close a Contact Form 7 Form After a Number of Entries" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
