borkolivic

Disable specific dates in Elementor Pro Form - Date field (Flatpickr)

Sep 4th, 2025 (edited)
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. add_action( 'wp_footer', function () {
  2.     if ( ! is_page('contact') ) return; // change 'contact' to your page slug or page ID
  3.  
  4.     ?>
  5.     <script>
  6.     (function($){
  7.       //Dates to disable (ISO format: YYYY-MM-DD)
  8.       const DISABLED_DATES = ["2025-09-10","2025-09-15","2025-12-25"];
  9.  
  10.       function applyDisable($scope){
  11.         const $inputs = ($scope && $scope.find)
  12.           ? $scope.find('.elementor-field-type-date input')
  13.           : $('.elementor-field-type-date input');
  14.  
  15.         $inputs.each(function(){
  16.           const input = this;
  17.  
  18.           (function waitForFP(tries){
  19.             const fp = input._flatpickr;
  20.             if (fp) {
  21.               const existing = Array.isArray(fp.config.disable) ? fp.config.disable : [];
  22.               fp.set('disable', existing.concat(DISABLED_DATES));
  23.  
  24.               fp.set('onClose', [
  25.                 function(selectedDates, dateStr) {
  26.                   if (!dateStr) return;
  27.                   const isDisabled = (fp.config.disable || []).some(rule => {
  28.                     if (typeof rule === 'string') return rule === dateStr;
  29.                     if (typeof rule === 'function') {
  30.                       return rule(fp.parseDate(dateStr, 'Y-m-d'), '', fp) === false;
  31.                     }
  32.                     return false;
  33.                   });
  34.                   if (isDisabled) {
  35.                     this.clear();
  36.                     this.input.blur();
  37.                     alert('That date is unavailable. Please choose another.');
  38.                   }
  39.                 }
  40.               ]);
  41.               return;
  42.             }
  43.             if (tries <= 0) return;
  44.             setTimeout(function(){ waitForFP(tries - 1); }, 100);
  45.           })(30);
  46.         });
  47.       }
  48.        
  49.       $(function(){ applyDisable($(document)); });
  50.  
  51.       if (window.elementorFrontend && elementorFrontend.hooks) {
  52.         elementorFrontend.hooks.addAction('frontend/element_ready/form.default', applyDisable);
  53.       }
  54.  
  55.       $(document).on('elementor/popup/show', function(){ applyDisable($(document)); });
  56.  
  57.     })(jQuery);
  58.     </script>
  59.     <?php
  60. });
  61.  
Advertisement
Add Comment
Please, Sign In to add comment