Last active
June 4, 2020 13:50
-
-
Save kylephillips/da5e4f082f89eb59b3c663fe1edc8b86 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) | |
$date_label = ''; // Here, we're creating some custom HTML, so we can format as needed | |
$date_day = date('D', strtotime('+' . $i . ' day')); // ex: Thu | |
$date_date = date('jS M', strtotime('+' . $i . ' day')); // ex: 4th Jun | |
$date_label = '<strong>' . $date_day . '</strong><br>' . $date_date; // The HTML for the choice | |
$dates[$i] = [ | |
'text' => $date_label, // The label | |
'value' => date('M d, Y', strtotime('+' . $i . ' day')) // The submitted value | |
]; | |
} | |
$field->choices = $dates; | |
} | |
return $form; | |
} | |
/** | |
* The following function would replace the above in order to completely remove Sundays | |
* This still outputs 14 days, but skips sundays | |
*/ | |
add_filter( 'gform_pre_render_1', 'populate_dates_remove_sundays' ); | |
add_filter( 'gform_pre_validation_1', 'populate_dates_remove_sundays' ); | |
add_filter( 'gform_pre_submission_filter_1', 'populate_dates_remove_sundays' ); | |
add_filter( 'gform_admin_pre_render_1', 'populate_dates_remove_sundays' ); | |
function populate_dates_remove_sundays( $form ) | |
{ | |
foreach ( $form['fields'] as $field ) { | |
if ( $field->type != 'radio' || strpos( $field->cssClass, 'date-radios' ) === false ) continue; | |
$dates = []; | |
$c = 15; | |
for ( $i = 1; $i < $c; $i++ ){ // (change to 0 and < 14 to include today) | |
$date_label = ''; // Here, we're creating some custom HTML, so we can format as needed' | |
$date_day = date('D', strtotime('+' . $i . ' day')); // ex: Thu | |
if ( $date_day == 'Sun' ) { | |
$c++; | |
continue; | |
} | |
$date_date = date('jS M', strtotime('+' . $i . ' day')); // ex: 4th Jun | |
$date_label = '<strong>' . $date_day . '</strong><br>' . $date_date; // The HTML for the choice | |
$dates[$i] = [ | |
'text' => $date_label, // The label | |
'value' => date('M d, Y', strtotime('+' . $i . ' day')) // The submitted value | |
]; | |
} | |
$field->choices = $dates; | |
} | |
return $form; | |
} | |
/** | |
* Disables "Sunday" choices in the dates | |
* @link https://docs.gravityforms.com/gform_field_choice_markup_pre_render/ | |
*/ | |
add_filter( 'gform_field_choice_markup_pre_render_1', 'disable_sundays', 10, 4 ); | |
function disable_sundays($choice_markup, $choice, $field, $value) | |
{ | |
if ( $field->type !== 'radio' ) return $choice_markup; | |
if ( strpos($choice['text'], 'Sun') == false ) return $choice_markup; | |
$choice_markup = str_replace( "type='radio'", "type='radio' disabled", $choice_markup ); | |
$choice_markup = str_replace( "class='", "class=' disabled ", $choice_markup ); | |
return str_replace( "type='radio'", "type='radio' disabled", $choice_markup ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment