¡Atención!

Este artículo contiene código PHP y está destinado a desarrolladores. Ofrecemos este código como cortesía, pero no proporcionamos soporte para personalizaciones de código o desarrollo de terceros.

Para obtener ayuda adicional, consulta el tutorial de WPBeginner sobre cómo añadir código personalizado.

Descartar

Descripción

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.

Origen

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

Más Información

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.

Ejemplos

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.

Solo recuerda cambiar el ID del formulario de 25 para que coincida con el ID específico del formulario en el que deseas ejecutar tu código. Eliminar esta comprobación lo ejecutaría para todos los formularios.

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