我在WordPress上的函数.php文件中设置了此过滤器,用于验证日期,因此取件日期时间早于返回日期和时间。当正确输入日期和时间时,它应该通过验证,但它一直失败,我只是无法理解这一点。它只是突然停止工作。我只能查明这一点以将服务器上的日期/时间设置为格林威治时间。
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
$pickupDate = null;
$returnDate = null;
foreach( $form['fields'] as & $field ) {
if ( $field->label == 'Pickup Date' ) {
$pickupDate = rgpost('input_' . $field->id);
//var_dump("Pickup Date:" . $pickupDate);
}
if ( $field->label == 'Pickup Time' ) {
$pickupDate .= ' ' . rgpost('input_' . $field->id);
//var_dump("Pickup Time:" . $pickupDate);
}
if ( $field->label == 'Return Date' ) {
$returnDate = rgpost('input_' . $field->id);
//var_dump("Return Date:" . $returnDate);
}
if ( $field->label == 'Return Time' ) {
$returnDate .= ' ' . rgpost('input_' . $field->id);
//var_dump("Return Time:" . $returnDate);
if (strtotime($returnDate) <= strtotime($pickupDate)) {
//var_dump($returnDate);
//var_dump($pickupDate);
$validation_result['is_valid'] = false;
$field->failed_validation = true;
$field->validation_message = 'Please ensure your selected Pickup Date and time is earlier than your Return Date and Time';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
if ( $message_format != 'html' ) {
return $email;
}
if (strpos($email['message'], 'Pickup Date') !== false) {
$email['message'] = explode('Pickup Date', $email['message']);
$email['message'][1] = str_replace('/', ', ', $email['message'][1]);
$email['message'][1] = str_replace('<, ', '</', $email['message'][1]);
$email['message'] = implode('Pickup Date', $email['message']);
}
$email['message'] = '<html>' . $email['message'] . '</html>';
return $email;
}, 10, 2 );
日期以这种格式被倾倒。
string(22) "Pickup Date:25/06/2020"
string(28) "Pickup Time:25/06/2020 08:00"
string(22) "Return Date:26/06/2020"
string(28) "Return Time:26/06/2020 08:00"
谁能发现问题出在哪里?
当您访问此URL时,您需要单击"需要报价"按钮。
您只需要从验证条件中删除 =。
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
$pickupDate = null;
$returnDate = null;
foreach( $form['fields'] as & $field ) {
if ( $field->label == 'Pickup Date' ) {
$pickupDate = rgpost('input_' . $field->id);
//var_dump("Pickup Date:" . $pickupDate);
}
if ( $field->label == 'Pickup Time' ) {
$pickupDate .= ' ' . rgpost('input_' . $field->id);
//var_dump("Pickup Time:" . $pickupDate);
}
if ( $field->label == 'Return Date' ) {
$returnDate = rgpost('input_' . $field->id);
//var_dump("Return Date:" . $returnDate);
}
if ( $field->label == 'Return Time' ) {
$returnDate .= ' ' . rgpost('input_' . $field->id);
//var_dump("Return Time:" . $returnDate);
if (strtotime($returnDate) < strtotime($pickupDate)) {
//var_dump($returnDate);
//var_dump($pickupDate);
$validation_result['is_valid'] = false;
$field->failed_validation = true;
$field->validation_message = 'Please ensure your selected Pickup Date and time is earlier than your Return Date and Time';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}