首页 > 解决方案 > 使用自定义响应联系表 7 将用户提交限制为每个用户一个

问题描述

我正在开发一个多语言竞赛网站,用户只能注册一次。我正在使用带有CFDB 插件的联系表 7。使用CFDB作者提供的以下代码,该函数检查表单是否已经提交;

function is_already_submitted($formName, $fieldName, $fieldValue) {
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $atts = array();
    $atts['show'] = $fieldName;
    $atts['filter'] = "$fieldName=$fieldValue";
    $atts['unbuffered'] = 'true';
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
        $found = true;
    }
    return $found;
}

以及验证表单的以下代码,确保没有重复的电子邮件(这就是我打算检测重复用户的方式)

function one_user_per_contest($result, $tag) {
    $formName = array('Contest Form', 'Contest form ES'); // This is the form name for English and spanish
    $fieldName = 'your-email'; // Name of the field to validate, which is the email field
    $errorMessage = "you've signed up already"; // error message
    $name = $tag->name;
    if ($name == $fieldName) {
        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}
add_filter('wpcf7_validate_email*', 'one_user_per_contest', 20, 2);

此验证工作正常,因为它不允许重复电子邮件,但我面临的问题是我希望得到一些指导,我希望将错误消息显示为响应。而不是回应说One or more fields have an error. Please check and try again.,我想让它说you've signed up already[见下图];

自定义响应结果

谢谢

标签: phpwordpresscontact-form-7

解决方案


推荐阅读