説明
について wpforms_datetime_date_dropdowns
は、日付フィールドのドロップダウンフォーマット内のラベルとオプションをフィルタリングします。
パラメータ
- 日付
- (必須)日付 ド ロ ッ プダ ウ ン で使われる ラ ベル と 数値範囲。
- フォームID
- (int) (必須)フォーム ID。
- フィールド
- (必須)日付フ ィ ール ド の値 と プ ロパテ ィ 。
ソース
wpforms/pro/includes/fields/class-date-time.php
詳細情報
について wpforms_datetime_date_dropdowns
フィルタは、日付のドロップダウンオプションを含む配列に適用されます。このフィルタを使用して、月・日・年の各ドロップダウンのラベルや、 各ドロップダウンに含まれるオプションをカスタマイズすることができます。
注:日、月、年の範囲は整数値しか受け付けない。
例
日程の制限
この例では、月が1月から6月まで、日が1日から15日まで、年が1980年から始まります。
この関数はまた、ラベルをデフォルトの MM/DD/YYYY
への M/D/Y
.
フォームIDを 25
を、コードを実行したい特定のフォームIDに一致させます。このチェックを外すと、すべてのフォームに対して実行されます。
/** * 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 );
スニペット内のフォームIDを、ご自身のフォームIDに合わせて更新することを忘れないでください。フォームID番号の見つけ方については、こちらのチュートリアルをご覧ください。
年数の拡大
もし、2023年で止まらないように年数を増やしたい場合は、このスニペットを使い、最大年数を 2050
をお望みのものに変えてください。
/** * 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 );