# Open a Contact Form 7 Form Only Between Two Dates

> Shows a Contact Form 7 form only during the dates you set, for example an application period, and blocks late submissions from pages left open.

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

## Code

```php
// Form ID => array( start, end ).
$wpide_cf7_windows = array(
    // 123 => array( '', '2026-12-31 23:59' ),
);
$wpide_cf7_window_message = 'This form is not accepting submissions at the moment.';

$wpide_cf7_is_open = function ( $id ) use ( $wpide_cf7_windows ) {
    if ( ! isset( $wpide_cf7_windows[ $id ] ) ) {
        return true;
    }

    $now   = current_time( 'timestamp' );
    $start = $wpide_cf7_windows[ $id ][0] ? strtotime( $wpide_cf7_windows[ $id ][0] ) : 0;
    $end   = $wpide_cf7_windows[ $id ][1] ? strtotime( $wpide_cf7_windows[ $id ][1] ) : PHP_INT_MAX;

    return $now >= $start && $now <= $end;
};

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

    $form = wpcf7_contact_form( $attr['id'] );

    return $form && ! $wpide_cf7_is_open( (int) $form->id() ) ? '<p class="wpide-form-closed" role="status">' . esc_html( $wpide_cf7_window_message ) . '</p>' : $output;
}, 10, 3 );

add_filter( 'wpcf7_spam', function ( $spam ) use ( $wpide_cf7_is_open ) {
    $form = function_exists( 'wpcf7_get_current_contact_form' ) ? wpcf7_get_current_contact_form() : null;

    return ( $form && ! $wpide_cf7_is_open( (int) $form->id() ) ) ? true : $spam;
}, 30 );
```

## Notes

Add form IDs with start and end dates (site timezone, Y-m-d H:i) to the list at the top; leave the start empty to open the form straight away.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Open a Contact Form 7 Form Only Between Two Dates" 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/
