WordPress Plugin functions, only one works?

So I have a plugin on wordpress with an in-built calendar.

The developer has given instructions on how to remove the option to select past dates, and also specific dates of your choosing.

Read More

http://support.themecatcher.net/quform-wordpress/guides/advanced/date-validation

I have followed the instructions, but perhaps I’m missing something. As only one of the functions work at a time, but never both at the same time.

Here is the code. Any help would be greatly appreciated. Thanks!


function my_prevent_past_dates($valid, $value, $element)
{
 $time = strtotime("{$value['year']}-{$value['month']}-{$value['day']}");

 if ($time < strtotime('today')) {
      $element->addError('Please choose a date in the future');
     $valid = false;
 }

 return $valid;
}

add_filter('iphorm_element_valid_iphorm_1_6', 'my_prevent_past_dates', 10, 3);

function my_datepicker_prevent_past_dates($options, $dpMinYear, $dpMaxYear, $element)
{
  return "{
     minDate: 0,
     maxDate: new Date({$dpMaxYear}, 12 - 1, 31)
    }";
}

add_filter('iphorm_datepicker_options_iphorm_1_6', 'my_datepicker_prevent_past_dates', 10, 4);



function my_get_dates_to_disable()
{
  return array(
        '2015-05-1',
        '2015-05-2',
        '2015-05-3',
        '2015-05-4',
        '2015-05-5',
        '2015-05-6',
  );
}

function my_prevent_specific_dates($valid, $value, $element)
{
  if ($valid) {
       $disabledDates = my_get_dates_to_disable();
       $submittedDate = "{$value['year']}-{$value['month']}-{$value['day']}";

     if (in_array($submittedDate, $disabledDates)) {
            $element->addError('That date is not available');
          $valid = false;
        }
  }

    return $valid;
}

add_filter('iphorm_element_valid_iphorm_1_6', 'my_prevent_specific_dates', 10, 3);

function my_datepicker_prevent_specific_dates($options, $dpMinYear, $dpMaxYear, $element)
{
   return "{
      beforeShowDay: function (date) {
           if (quformDisabledDates) {
                for (var i = 0; i < quformDisabledDates.length; i++) {
                   var parts = quformDisabledDates[i].split('-');
                 if (date.getFullYear() == parts[0] && (date.getMonth()+1) == parts[1] && date.getDate() == parts[2]) {
                      return [false];
                  }
              }
          }
           return [true];
        }
  }";
}

add_filter('iphorm_datepicker_options_iphorm_1_6', 'my_datepicker_prevent_specific_dates', 10, 4);

function my_print_dates_to_disable()
{
   ?>
   <script type="text/javascript">
   var quformDisabledDates = <?php echo json_encode(my_get_dates_to_disable()); ?>;
  </script>
   <?php
}
add_action('wp_head', 'my_print_dates_to_disable');

Related posts

Leave a Reply

1 comment

  1. You can combine my_prevent_past_dates with my_prevent_specific_dates, and my_datepicker_prevent_past_dates with my_datepicker_prevent_specific_dates.

    function my_get_dates_to_disable()
    {
      return array(
            '2015-05-1',
            '2015-05-2',
           '2015-05-3',
           '2015-05-4',
           '2015-05-5',
         '2015-05-6',
      );
    }
    
    
    function my_prevent_bad_dates($valid, $value, $element)
    {
     $time = strtotime("{$value['year']}-{$value['month']}-{$value['day']}");
    
     if ($time < strtotime('today')) {
         $element->addError('Please choose a date in the future');
         $valid = false;
     } else {
         $disabledDates = my_get_dates_to_disable();
         $submittedDate = "{$value['year']}-{$value['month']}-{$value['day']}"; 
    
         if (in_array($submittedDate, $disabledDates)) {
              $element->addError('That date is not available');
              $valid = false;
         }     
     }
    
     return $valid;
    }
    
    add_filter('iphorm_element_valid_iphorm_1_6', 'my_prevent_bad_dates', 10, 3);
    
    
    function my_datepicker_prevent_bad_dates($options, $dpMinYear, $dpMaxYear, $element)
    {
      return "{
         minDate: 0,
         maxDate: new Date({$dpMaxYear}, 12 - 1, 31),
         beforeShowDay: function (date) {
           if (quformDisabledDates) {
             for (var i = 0; i < quformDisabledDates.length; i++) {
               var parts = quformDisabledDates[i].split('-');
               if (date.getFullYear() == parts[0] && (date.getMonth()+1) == parts[1] && date.getDate() == parts[2]) {
                 return [false];
               }
             }
            }
            return [true];
          }     
        }";
    }
    
    add_filter('iphorm_datepicker_options_iphorm_1_6', 'my_datepicker_prevent_bad_dates', 10, 4);
    
    function my_print_dates_to_disable()
    {
       ?>
       <script type="text/javascript">
       var quformDisabledDates = <?php echo json_encode(my_get_dates_to_disable()); ?>;
      </script>
       <?php
    }
    add_action('wp_head', 'my_print_dates_to_disable');