简体   繁体   English

需要自定义wordpress联系人form-7表单字段验证

[英]Need to customize wordpress contact form-7 form fields Validation

In my Wordpress website I installed the Contact Form-7 plugin, and have been having a problem. 在我的Wordpress网站上,我安装了Contact Form-7插件,并且遇到了问题。 I think there is no special validation for text fields. 我认为对于文本字段没有特殊的验证。 Also for phone fields, Contact Form-7 plugin only provides basic validation for not null or blank fields. 同样对于电话字段,Contact Form-7插件仅提供对not nullblank字段的基本验证。

My question is: for those issues what should I do to fix them? 我的问题是:对于那些问题,我应该怎么解决? I could either modify the core plugin file OR create my own contact form. 我可以修改核心插件文件,也可以创建自己的联系表。

In Contact Form 7, a user-input validation is implemented as a filter function. 在联系表7中,用户输入验证被实现为过滤器功能。 The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}. 用于验证的过滤器挂钩根据表单标签的类型而有所不同,并确定为:wpcf7_validate_ + {表单标签的类型}。 So, for text form-tags, the filter hook wpcf7_validate_text is used. 因此,对于文本表单标签,使用了过滤器挂钩wpcf7_validate_text。 Likewise, wpcf7_validate_email* is used for email* form-tags. 同样,wpcf7_validate_email *用于email *表单标签。

Let's say you have the following email fields in a form: 假设您在表单中具有以下电子邮件字段:

  Email:         [email* your-email]
  Confirm email: [email* your-email-confirm]

The following listing shows code that verifies whether the two fields have identical values. 以下清单显示了验证两个字段是否具有相同值的代码。

add_filter('wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2);

function custom_email_confirmation_validation_filter($result, $tag) {
    $tag = new WPCF7_Shortcode($tag);

    if ('your-email-confirm' == $tag->name) {
        $your_email = isset($_POST['your-email']) ? trim($_POST['your-email']) : '';
        $your_email_confirm = isset($_POST['your-email-confirm']) ? trim($_POST['your-email-confirm']) : '';

        if ($your_email != $your_email_confirm) {
            $result->invalidate($tag, "Are you sure this is the correct address?");
        }
    }
    return $result;
}

Two parameters will be passed to the filter function: $result and $tag. 两个参数将传递给过滤器函数:$ result和$ tag。 $result is an instance of WPCF7_Validation class that manages a sequence of validation processes. $ result是WPCF7_Validation类的实例,该类管理一系列验证过程。 $tag is an associative array composed of given form-tag components; $ tag是由给定的form-tag组件组成的关联数组; as you saw in the previous recipe, you can use WPCF7_Shortcode class to handle this type of data. 如您在前面的食谱中所见,可以使用WPCF7_Shortcode类来处理此类数据。

Look through the inside of the filter function. 查看过滤器功能的内部。 First, check the name of the form-tag to ensure the validation is applied only to the specific field (your-email-confirm). 首先,检查表单标签的名称,以确保仅将验证应用于特定字段(您的电子邮件确认)。

The two email field values are then compared, and if they don't match, $result->invalidate() will be called. 然后比较两个电子邮件字段值,如果它们不匹配,则将调用$ result-> invalidate()。 You need to pass two parameters to the invalidate() method: the first parameter should be the $tag variable, and the second parameter is the validation error message that you want the field to display. 您需要将两个参数传递给invalidate()方法:第一个参数应该是$ tag变量,第二个参数是希望字段显示的验证错误消息。

Lastly, don't forget to return the $result. 最后,不要忘记返回$ result。

新的联系表7插件提供了其最新更新的内置验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM