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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.