使用自定义回复联系表7将用户提交的信息限制为每个用户一次

时间:2019-04-15 09:51:03

标签: php wordpress contact-form-7

我正在一个多语言竞赛网站上,该网站只允许用户注册一次。我正在将contact form 7CFDB plugin一起使用。使用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 [见下图];

Custom Response Result

谢谢

0 个答案:

没有答案