我正在使用Contact Form 7插件,希望用户填写电子邮件或电话字段以发送消息。
我尝试了以下主题的解决方案:https://wordpress.org/support/topic/plugin-contact-form-7-two-required-fields-but-only-one-must-be-filled/,但似乎不再起作用。
我也尝试使用以下代码片段
1
function wpq_validate( $result ) {
$form = WPCF7_Submission::get_instance();
$email = $form->get_posted_data('email');
$telephone = $form->get_posted_data('phone');
if( empty($email) && empty($telephone) ) {
$result->invalidate('email', 'Either one of these fields must be filled.
Please try again.' );
$result->invalidate('phone', 'Either one of these fields must be filled.
Please try again.' );
}
return $result;
}
2
add_filter( 'wpcf7_validate', 'wpq_validate' );
function wpq_validate( $result ) {
$email = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING );
$telephone = filter_input( INPUT_POST, 'phone', FILTER_SANITIZE_STRING );
$error_msg = 'Either one of these fields must be filled. Please try again."';
if( empty($email) && empty($telephone) ) {
$result['valid'] = false;
$result['reason']['email'] = $error_msg;
$result['reason']['phone'] = $error_msg;
}
return $result;
}
我想念什么?有没有人尝试使用最新版本的Contact Form 7制作这样的东西?