Last active
June 3, 2020 20:57
-
-
Save kylephillips/7b5383e59888083772b6c9d8a6406f87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filters radio choices to include the next 14 days | |
* | |
* Place in the theme's functions.php file | |
* @link https://docs.gravityforms.com/dynamically-populating-drop-down-fields/ | |
*/ | |
add_filter( 'gform_pre_render_1', 'populate_dates' ); | |
add_filter( 'gform_pre_validation_1', 'populate_dates' ); | |
add_filter( 'gform_pre_submission_filter_1', 'populate_dates' ); | |
add_filter( 'gform_admin_pre_render_1', 'populate_dates' ); | |
function populate_dates( $form ) | |
{ | |
foreach ( $form['fields'] as $field ) { | |
if ( $field->type != 'radio' || strpos( $field->cssClass, 'date-radios' ) === false ) continue; | |
$dates = []; | |
for ( $i = 1; $i < 15; $i++ ){ // (change to 0 and <14 to include today) | |
$dates[$i] = [ | |
'text' => date('M d, Y', strtotime('+' . $i . ' day')), // The label | |
'value' => date('M d, Y', strtotime('+' . $i . ' day')) // The submitted value | |
]; | |
} | |
$field->choices = $dates; | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above filters the radio field with a css class of "date-radios" under a form with an ID of 1.
The list of choices is returned to include the next 14 days.
Date formatting can be updated as needed for the label and value.