Block Date Ranges via Exceptions

Code

Filename: gpld-block-date-ranges.php

<?php
/**
 * Gravity Perks // GP Limit Dates // Block Date Ranges via Exceptions
 */
add_filter( 'gpld_limit_dates_options_FORMID_FIELDID', 'gpld_except_date_ranges', 10, 3 );
function gpld_except_date_ranges( $options, $form, $field ) {
	/**
	 * Format: Start Date, End Date
	 */
	$ranges = array(
		array( '2016-07-15', '2017-01-01' ),
		array( '2020-12-10', '2020-12-20' ),
	);

	// do not modify below this line
	foreach ( $ranges as $range ) {
		$start_date = new DateTime( $range[0] );
		$end_date   = new DateTime( $range[1] );
		$period     = new DatePeriod( $start_date, new DateInterval( 'P1D' ), $end_date );

		foreach ( $period as $date ) {
			$options['exceptions'][] = $date->format( 'm/d/Y' );
		}
	}

	$options['exceptionMode'] = 'disable';

	return $options;
}

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.