Skip to content

Instantly share code, notes, and snippets.

@kylephillips
Last active June 3, 2020 20:57
Show Gist options
  • Save kylephillips/7b5383e59888083772b6c9d8a6406f87 to your computer and use it in GitHub Desktop.
Save kylephillips/7b5383e59888083772b6c9d8a6406f87 to your computer and use it in GitHub Desktop.
<?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;
}
@kylephillips
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment