【问题标题】:Contact Form 7 allow duplicate after 24 hours联系表格 7 允许 24 小时后重复
【发布时间】:2021-01-06 23:58:29
【问题描述】:

我正在使用联系表 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 my_validate_email($result, $tag) {
    $formName = 'email_form'; // Change to name of the form containing this field
    $fieldName = 'email_123'; // Change to your form's unique field name
    $errorMessage = 'Email has already been submitted'; // Change to your error message
    $name = $tag['name'];
    if ($name == $fieldName) {
        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}

如果用户在 24 小时后重试,我们现在需要允许重复输入。

我们的建议是运行一个 cron 作业来标记超过 24 小时的条目,然后允许用户继续。我们添加了一个新的表列 (allow_duplicate) 来标记条目。

任何关于如何构建 functions.php 验证的建议将不胜感激。

【问题讨论】:

  • 是您在电子邮件中检查的唯一字段吗?
  • 是的,但架构足够灵活,我可以更改它。有什么原因吗?
  • 只是想另一种方法来做到这一点。使用瞬变代替 CFDB
  • 我对瞬变不熟悉。然而,在这一点上的任何建议都将非常有价值

标签: database wordpress validation duplicates contact-form-7


【解决方案1】:

所以我个人...我会使用Transient API。使用 Transients,您可以将过期时间设置为 24 小时,并且不再需要 cron 任务。首先...使用钩子wpcf7_before_send_mail 设置瞬态,然后使用您的验证过滤器检查瞬态是否存在。临时文件将在首次提交后 24 小时过期。

在函数dd_handle_form_submission中设置您的联系表单ID

// capture data after contact form submit
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' ); 
function dd_handle_form_submission( $contact_form ) {
    // Check Form ID
    if( 2744 !== $contact_form->id() ) return; // Use your form ID here
    $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
        // Set the Transient
        $transient_name = 'cf7_has_submitted_' . $posted_data['email_123'];
        set_transient($transient_name, true, 1 * DAY_IN_SECONDS);
    }
    return;
}

// Custom Validation Filter
add_filter( 'wpcf7_validate_email*', 'my_validate_email', 20, 2);
function my_validate_email($result, $tag) {
    $fieldName = 'email_123'; // Change to your form's unique field name
    $errorMessage = 'Email has already been submitted'; // Change to your error message
    $name = $tag['name'];
    if ($name == $fieldName) {
        // Create the Transient Name to lookup with the email field
        $transient = 'cf7_has_submitted_' . $_POST[$name];
        // Lookup transient
        if (false !== get_transient($transient)) $result->invalidate($tag, $errorMessage);
    }
    return $result;
}

【讨论】:

  • 谢谢,问题解决了。
猜你喜欢
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 2013-10-20
  • 1970-01-01
相关资源
最近更新 更多