Atenção!

Este artigo contém código PHP e destina-se a desenvolvedores. Oferecemos este código como uma cortesia, mas não fornecemos suporte para personalizações de código ou desenvolvimento de terceiros.

Para orientação extra, consulte o tutorial do WPBeginner sobre como adicionar código personalizado.

Dispensar

Descrição

The wpforms_datetime_date_dropdowns filters labels and options within the date field’s dropdown format.

Parâmetros

$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.

Fonte

wpforms/pro/includes/fields/class-date-time.php

Mais Informações

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.

Exemplos

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.

Lembre-se apenas de alterar o ID do formulário de 25 para corresponder ao ID específico do formulário em que você deseja executar seu código. Remover essa verificação seria executado para todos os formulários.

/**
 * 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 );

Article Reference: Customize the Date Time Field Date Options