要在Contact Form 7中验证多个电子邮件字段,您可以使用以下代码示例来实现:
首先,打开您的WordPr
ess
网站的functions.php文件。
添加以下代码到functions.php文件中:
add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 20, 2 );
function custom_email_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'your-email' == $tag->name ) {
$email1 = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
$email2 = isset( $_POST['your-email2'] ) ? trim( $_POST['your-email2'] ) : '';
if ( ! empty( $email1 ) && ! empty( $email2 ) && $email1 != $email2 ) {
$result->invalidate( $tag, "Email fields do not match." );
return $result;
注意:请根据您在Contact Form 7中使用的字段名称进行适当的更改。在上面的示例中,“your-email”和“your-email2”是电子邮件字段的名称。
保存并上传functions.php文件到您的WordPress网站。
现在,当用户提交表单时,将会验证两个电子邮件字段是否匹配。如果不匹配,将会显示错误消息“Email fields do not match.”。您可以根据自己的需求自定义错误消息。
请确保在进行任何更改之前备份functions.php文件,并使用适当的代码编辑器进行编辑。