所以我在wordpress上有一个带内置日历的插件。
开发人员已说明如何删除选择过去日期的选项,以及您选择的具体日期。
http://support.themecatcher.net/quform-wordpress/guides/advanced/date-validation
我按照说明操作,但也许我错过了一些东西。因为其中只有一个功能一次工作,但从不同时工作。
这是代码。任何帮助将不胜感激。谢谢!
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');
答案 0 :(得分:0)
您可以将my_prevent_past_dates与my_prevent_specific_dates和my_datepicker_prevent_past_dates与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');