Universal Date Exceptions

Except dates across all Date fields on all forms. You can also skip applying exceptions for certain forms and fields.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gpld-universal-exceptions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
 * Gravity Perks // Limit Dates // Universal Date Exceptions
 * https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 *
 * Except dates across all Date fields on all forms. You can also skip applying exceptions for certain forms and fields.
 */
add_filter( 'gpld_limit_dates_options', function( $field_options, $form, $field ) {
	// Optional exclusions: add IDs to skip applying exceptions.
	$exclude = array();
	/*
	$exclude = array(
		array(
			'form_id' => 123, // Exclude all date fields on form 123.
		),
		array(
			'form_id'  => 124,
			'field_id' => 5, // Exclude field 5 on form 124.
		),
	);
	*/

	$form_id           = (int) $form['id'];
	$excluded_field_ids = array();

	foreach ( $exclude as $rule ) {
		if ( empty( $rule['form_id'] ) || (int) $rule['form_id'] !== $form_id ) {
			continue;
		}

		if ( empty( $rule['field_id'] ) ) {
			return $field_options;
		}

		$excluded_field_ids[] = (int) $rule['field_id'];
	}

	foreach ( $field_options as $field_id => &$_field_options ) {
		if ( in_array( (int) $field_id, $excluded_field_ids, true ) ) {
			continue;
		}

		if ( ! isset( $_field_options['exceptions'] ) || ! is_array( $_field_options['exceptions'] ) ) {
			$_field_options['exceptions'] = array();
		}
		// Add as many exceptions as you need here.
		$_field_options['exceptions'][] = '04/01/2026';
		$_field_options['exceptions'][] = '07/04/2026';
		$_field_options['exceptions'][] = '12/25/2026';
	}

	return $field_options;
}, 10, 3 );

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.