Last active
November 28, 2023 15:16
-
-
Save railmedia/0b76a18282aaef7cd74623d9682449c6 to your computer and use it in GitHub Desktop.
GravityForms usage of gform_datepicker_options_pre_init JS filter
This file contains hidden or 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
| /* | |
| * Create a JS file and upload it to your child theme | |
| * Assuming this directory structure: /wp-content/themes/<your_child_theme_folder/assets/js/gform_filters.js | |
| */ | |
| //PHP Side - functions.php or some other way | |
| add_action( 'wp_enqueue_scripts', 'gform_datepicker_options_pre_init_filter' ); | |
| function gform_datepicker_options_pre_init_filter() { | |
| //Add here any required conditionals to load below script conditionally | |
| wp_enqueue_script( 'child-gform', get_stylesheet_directory_uri() . '/assets/js/gform_filters.js', array('jquery'), null, true ); | |
| } | |
| /* | |
| * JS Side - content of gform_filters.js file. | |
| * Using https://docs.gravityforms.com/gform_datepicker_options_pre_init/#h-usage as reference | |
| * change formId and fieldId values depending on your own form | |
| * change disabledDays array, depending on the days you wish to disable | |
| */ | |
| document.addEventListener('DOMContentLoaded', function() { | |
| if( gform ) { | |
| gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) { | |
| if ( formId == 12 && fieldId == 14 ) { | |
| var disabledDays = ['06/15/2014', '07/01/2014', '07/15/2014']; | |
| optionsObj.beforeShowDay = function(date) { | |
| var checkdate = jQuery.datepicker.formatDate('mm/dd/yy', date); | |
| return [disabledDays.indexOf(checkdate) == -1]; | |
| }; | |
| } | |
| return optionsObj; | |
| }); | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment