Extensions

Search Extensions

Custom Datepicker Start Date

Past dates are not able to be selectedWant to disallow users creating events that started in the past?! This extension is for you!

With this extension activated, your users will not be able to pick a day in the past.

It works for existing and new events and in both wp-admin and the Community Events event add/edit form (if applicable).

Via a filter (see below), you can also set the start date’s maxDate, such as being no further out than 1 month in the future.

GIF of the minDate applied in Community Events new event and wp-admin existing event

GIF of the minDate applied to the Community Events new event form and the wp-admin event edit screen for an existing event

Filter Hooks

  • tribe_ext_start_datepicker_cap_allowed_any_start_date
    • The capability required to have this script not load.
    • Default: manage_options
  • tribe_ext_start_datepicker_min_date_interval
    • The time prior to today to allow as the minimum start date, such as 'P7D' for 7 days in the past.
    • See the PHP DateInterval class for acceptable formats.
  • tribe_ext_start_datepicker_max_date_interval
    • The time forward from today to allow as the maximum start date, such as 'P1M' for 1 month in the future.
    • See the PHP DateInterval class for acceptable formats.
  • tribe_ext_start_datepicker_time_zone
    • A named time zone used for date calculations–the event time zone or the WordPress site time zone, depending on your settings.
  • tribe_ext_start_datepicker_load_script_wp_admin
    • Whether or not this script should run within wp-admin.
    • For example, you may want to set this to FALSE so the extension only affects the Community Events event add/edit form but not users with access to add/edit events in wp-admin.
  • tribe_ext_start_datepicker_load_script_ce_form
    • Whether or not this script should run for the Community Events event add/edit form.

Example Filtering minDate and maxDate

You can add this code if you want the allowable start date to be within 7 days prior to today and 1 month after today:

/**
 * Filter the minDate and maxDate for The Events Calendar's "Custom Datepicker
 * Start Date" extension, which requires PHP 5.3+.
 *
 * 7 days prior to Today through 1 month forward from today.
 *
 * @see Tribe__Extension__Custom_Datepicker_Start_Date::get_min_date()
 * @see Tribe__Extension__Custom_Datepicker_Start_Date::get_max_date()
 *
 * @link https://theeventscalendar.com/extensions/custom-datepicker-start-date/
 * @link https://secure.php.net/manual/class.dateinterval.php The format to follow.
 * @link https://gist.github.com/cliffordp/a699255635f7266967284005d979a89d
 */
add_filter( 'tribe_ext_start_datepicker_min_date_interval', function() {
    return 'P7D';
} );
add_filter( 'tribe_ext_start_datepicker_max_date_interval', function() {
    return 'P1M';
} );

Please reference our Best Practices for Implementing Custom Code Snippets article for how to implement code snippets like this.

Notes

  • Administrators (users with the “manage_options” capability) are not affected by this extension; they can select any start date they want.
  • Works with Events Calendar PRO‘s recurrence date selectors, too.
  • The end date is not affected by this extension because it should already have logic to be relative to the chosen start date.
  • The start timepicker is not affected so someone technically could create an event in the past.
    • For example: it’s 10am and a user creates an event that started today at 8am.
  • If the event’s or site’s time zone is in a different day than the user’s own time zone, the datepicker may appear to allow selecting yesterday’s date but trying to select it will result in an error.
    • Really, this is correct (although less-than-ideal UX) because they’re selecting a date in the site’s time zone, which is already “their tomorrow date”.
    • For example, if a user is in UTC-1 but the site’s time zone is UTC+1 (2 hour difference) and it’s currently 11 PM in UTC, the site’s “today” is a different date than the user’s “today”.
  • All protection against setting an invalid start date is handled via JavaScript. There is no PHP validation. To implement your own would require a fair bit of custom coding that wouldn’t rely on this extension’s code. Such functionality is not planned for this extension’s future.
  • If the time zone is not a named time zone, “UTC” will be used.

Requirements

Changelog

Version 1.0.1

  • March 31, 2018
  • Fix an improperly-translated string.

Version 1.0.0

  • March 30, 2018
  • Initial version.

Download Extension

👋 Heads up! We provide limited support for extensions, but you can still open a ticket over at our Help Desk to report any issues.
Install Instructions