Description
The wpforms_datetime_date_dropdowns
filters labels and options within the date field’s dropdown format.
Parameters
- $dates
- (array) (Required) Labels and number ranges used in date dropdowns.
- $form_id
- (int) (Required) Form ID.
- $field
- (array) (Required) Date field values and properties.
Source
wpforms/pro/includes/fields/class-date-time.php
More Information
The wpforms_datetime_date_dropdowns
filter is applied to an array containing date dropdown options. It can be used to customize the labels for the month, day, and year dropdowns, as well as which options are included within each of these dropdowns.
Note: Ranges for days, months, and years can accept only integer values.
Examples
Limiting the dates
In this example shown below the function will limit the months to January through June, the days from the 1st to the 15th and the year will start at 1980.
The function is also changing the labels from the default MM/DD/YYYY
to M/D/Y
.
Just remember to change the form ID from 25
to match the specific form ID you’re wanting to run your code on. Removing that check would run for all forms.
/** * Filters labels and options within the date field’s dropdown format. * @link https://wpforms.com/developers/wpforms_datetime_date_dropdowns/ * * @param array $dates Months, Days, and Years arguments. * @param int $form_id Form ID. * @param array $field Date field values and properties. * * @return array */ function wpf_dev_datetime_date_dropdowns( $dates, $form_id, $field ) { // Only run on my form with ID = 25 if ( absint( $form_id ) !== 25 ) { return $ranges; } $ranges = array( 'months' => range( 1, 6 ), 'days' => range( 1, 15 ), 'years' => range( date( 'Y' ), 1980 ), 'months_label' => esc_html__( 'M', 'wpforms' ), 'days_label' => esc_html__( 'D', 'wpforms' ), 'years_label' => esc_html__( 'Y', 'wpforms' ), ); return $ranges; } add_filter( 'wpforms_datetime_date_dropdowns', 'wpf_dev_datetime_date_dropdowns', 10, 3 );
Please remember to update the form ID in the snippet to match your own form ID. If you need help in finding your form ID number, please see this tutorial.
Expanding the years
If you would like to expand the years so they do not stop with 2023, please use this snippet and change the max year of 2050
to what you’d like.
/** * Filters labels and options within the date field’s dropdown format. * @link https://wpforms.com/developers/wpforms_datetime_date_dropdowns/ * * @param array $dates Months, Days, and Years arguments. * @param int $form_id Form ID. * @param array $field Date field values and properties. * * @return array */ function wpf_dev_datetime_date_dropdowns( $dates, $form_id, $field ) { $ranges = array( 'months' => range( 1, 12 ), 'days' => range( 1, 31 ), 'years' => range( date( 'Y' ), 2050 ), 'months_label' => esc_html__( 'MM', 'wpforms' ), 'days_label' => esc_html__( 'DD', 'wpforms' ), 'years_label' => esc_html__( 'YYYY', 'wpforms' ), ); return $ranges; } add_filter( 'wpforms_datetime_date_dropdowns', 'wpf_dev_datetime_date_dropdowns', 10, 3 );
Related
Article Reference: Customize the Date Time Field Date Options